- This topic is empty.
-
AuthorPosts
-
July 14, 2014 at 9:43 pm #175436
Anonymous
InactiveJuly 14, 2014 at 10:53 pm #175438__
ParticipantNope, missed it.
Sorry!I’ll have a chance tomorrow evening to get back to this.
July 15, 2014 at 5:24 pm #175509__
Participantthe success page
Let’s start with this. Before we do, though, let’s remove the user notice from the
htmlMarkup
method, and give it its own method so it can be printed separately. Ifpublic function htmlMarkup_userNotice(){ return ($this->_notice)? "<p>".htmlspecialchars( $this->_notice )."</p>": false; }
Now, making the form submit to another page is actually very simple. We don’t have to change anything in the class definition — only how we use it. On your contact page, where you call the
htmlMarkup
method:// how it is now <div id="form"> <?= $contactForm->htmlMarkup(); ?> </div>
The
htmlMarkup
method accepts an argument$action
, which specifies the value for the form’saction
attribute (i.e., the URL to submit the form to).// change it to the [relative] URL for your "success" page <div id="form"> <?= $contactForm->htmlMarkup( "add/desired/URL/here.php" ); ?> </div>
…and then create a new PHP page at that URL. At a minimum, you’ll want to show the user the notice from the form submission:
<?php // start off exactly as before; // include the class definition, // make sure a session exists, // create a contactForm object. include_once "contactMichael1961.php"; session_start(); $contactForm = new contactMichael1961( $_POST ); ?> <-- all your HTML --> <-- then put the success/failure notice --> <?= $contactForm->htmlMarkup_userNotice() ?> <-- all the rest of your HTML -->
Note that there is only a notice if the form was valid, and we tried to send the email. If it was not valid, the notice will be empty (
htmlMarkup_userNotice
will returnfalse
).So, if
htmlMarkup_userNotice
returnsfalse
, we know we need to print the form instead (so the user can make corrections).// same as above … ?> <-- all your HTML --> <-- then put the success/failure notice --> <?php $notice = $contactForm->htmlMarkup_userNotice(); if( $notice ){ echo $notice; } else{ echo $contactForm->htmlMarkup(); } ?> <-- all the rest of your HTML -->
That whole if/else statement can be rewritten as
<?= $contactForm->htmlMarkup_userNotice()?: $contactForm->htmlMarkup() ?>
…if desired.
We’ll fix the anti-spam challenge next.
July 16, 2014 at 5:15 am #175549Anonymous
InactiveGreetings Traq,
I changed this:
return $this->_notice.str_replace( $placeholders,$replacements,$this->_htmlMarkup );
to this:
return str_replace( $placeholders,$replacements,$this->_htmlMarkup );
and added this on the same form.php after initially putting it on the wrong php:
public function htmlMarkup_userNotice(){ return ($this->_notice)? "<p>".htmlspecialchars( $this->_notice )."</p>": false;
I have added this to the contact page:
<div> <?= $contactForm->htmlMarkup( "http://www.wslmf.org/new/test/success.php" ); ?> </div>
I am not sure I have the above correct however.
I don’t believe I am getting this in the correct place either
<?= $contactForm->htmlMarkup_userNotice() ?>
as the form submits, but doesn’t direct to the success.php page.
Best Regards.
July 16, 2014 at 9:18 pm #175622__
ParticipantI have added this to the contact page
Could you link to your current contact page?
When I visit that URL directly, I do see your “success” message.July 16, 2014 at 11:31 pm #175629Anonymous
InactiveOops, Sorry about that!
July 17, 2014 at 11:10 am #175703__
ParticipantHere’s the current page.
Your form still has
action=?
. Are you sure you used<?= $contactForm->htmlMarkup( "http://www.wslmf.org/new/test/success.php" ); ?>
July 17, 2014 at 7:48 pm #175729Anonymous
InactiveGreetings Traq,
Yes, on the form page itself I have.
<div> <?= $contactForm->htmlMarkup( "http://www.wslmf.org/new/test/success.php" ); ?> </div>
The form submits but doesn’t go to the success page.
Best Regards.
July 17, 2014 at 9:17 pm #175730__
ParticipantThat’s odd. When I inspect the HTML source, I see:
<form class=cform action=? method=POST>
When you pass a URL to the
htmlMarkup
method (as above), then it should show in theaction
attribute (like so):<form class=cform action="http://www.wslmf.org/new/test/success.php" method=POST>
It works properly when I test it locally. Can you verfiy your
htmlMarkup
method matches the code in the gist?public function htmlMarkup( $action=null ){ // where to submit the form? (use current page url as default) if( $action === null ){ $action = "?"; } $pr["%action_url%"] = $action; // for each value {name}: // create a placeholder key {name}_value; // escape special html chars in the value and "quote" it // (the $v? allows us to skip the htmlspecialchars() function if the value is empty) foreach( $this->_values as $k=>$v ){ $pr["%{$k}_value%"] = $v? htmlspecialchars( $v ): ''; } // same as above, but the value goes in an error message foreach( $this->_errors as $k=>$v ){ $pr["%{$k}_error%"] = $v? '<p class=error>'.htmlspecialchars( $v ).'</p>': ''; } // make a list of <option>s for each possible recipient // (but we do not show the email addresses to the user) $pr["%sendto_options%"] = ""; foreach( $this->_recipients as $name=>$email ){ $pr["%sendto_options%"] .= "<option>".htmlspecialchars( $name )."</option>"; } // add a randomly selected antispam question $pr["%antispam_question%"] = htmlspecialchars( $this->_generateAntispam() ); // add a security token $pr["%token%"] = $this->_generateToken(); // the keys are the placeholders; the values are the replacements $placeholders = array_keys( $pr ); $replacements = array_values( $pr ); // replace placeholders in the html with their replacement values return str_replace( $placeholders,$replacements,$this->_htmlMarkup ); }
July 17, 2014 at 9:48 pm #175732Anonymous
InactiveThat’s odd. When I inspect the HTML source, I see:
<form class=cform action=? method=POST>
Well there’s a perfectly reasonable explanation for that, and it is that I put the url in the wrong place.
I thought the url for the success page was to go in the coding within
<div id=form> <?= $contactForm->htmlMarkup("http://www.wslmf.org/new/test/success.php"); ?> </div>
I’ve removed that and it is like this
<div id=form> <?= $contactForm->htmlMarkup(); ?> </div>
I changed this
<form class=cform action=? method=POST>
to this
<form class=cform action="http://www.wslmf.org/new/test/success.php" method=POST>
I am directed to the success page now after submitting the form, and hope I have everything right now.
July 18, 2014 at 12:17 am #175733Anonymous
InactiveWell, since I made the above changes I am directed to the success page but receive nothing in my inbox. I’ve tried to work back to when it at least sent something, but zilch.
July 18, 2014 at 7:59 am #175748__
ParticipantWell there’s a perfectly reasonable explanation for that, and it is that I put the url in the wrong place.
Actually, no; the URL is supposed to go in the method call:
<div id=form> <?= $contactForm->htmlMarkup("http://www.wslmf.org/new/test/success.php"); ?> </div>
Though if you had
action=?
hard-coded in the email template, that would cause the problem. The email template should look like this:<form class=cform action="%action_url%" method=POST>
…which would allow the
htmlMarkup
method to properly parse the placeholders.Well, since I made the above changes I am directed to the success page but receive nothing in my inbox.
Can you show the PHP source code for your success page?
July 18, 2014 at 8:26 am #175751Anonymous
InactiveGreetings Traq,
I seem to have left this out
<?php $notice = $contactForm->htmlMarkup_userNotice(); if( $notice ){ echo $notice; } else{ echo $contactForm->htmlMarkup(); } ?>
but am not sure where it should go.
I’ve made corrections to the others.
Many Thanks!
July 18, 2014 at 10:24 pm #175781__
ParticipantThat was a suggestion for the “success” page. Basically, you check if there was a success message (indicating the form was valid, and you tried to email the submission). If there is not (presumably because there were validation errors), you show the form again so the user can make corrections.
Actually, if you want a custom success message —i.e., the one you wrote on your page— we can change the script so it uses it.
July 18, 2014 at 11:17 pm #175782Anonymous
InactiveGreetings Traq,
I’m still not getting emails from the form, just directed to the success page.
Is there coding that’s supposed to be in the success page too?
Best Regards.
-
AuthorPosts
- The forum ‘Other’ is closed to new topics and replies.