Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Other Can someone recommend a customizable contact form, please?

  • This topic is empty.
Viewing 15 posts - 136 through 150 (of 225 total)
  • Author
    Posts
  • #175436
    Anonymous
    Inactive

    @traq

    Just wondering if you saw my previous message?

    Best Regards.

    #175438
    __
    Participant

    Nope, missed it.
    Sorry!

    I’ll have a chance tomorrow evening to get back to this.

    #175509
    __
    Participant

    the 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. If

        public 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’s action 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 return false).

    So, if htmlMarkup_userNotice returns false, 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.

    #175549
    Anonymous
    Inactive

    Greetings Traq,

    I changed this:

    return $this-&gt;_notice.str_replace( $placeholders,$replacements,$this-&gt;_htmlMarkup );
    

    to this:

    return str_replace( $placeholders,$replacements,$this-&gt;_htmlMarkup );
    

    and added this on the same form.php after initially putting it on the wrong php:

    public function htmlMarkup_userNotice(){
    return ($this-&gt;_notice)?
    "<p>".htmlspecialchars( $this-&gt;_notice )."</p>":
    false;
    

    I have added this to the contact page:

      <div>
     &lt;?= $contactForm-&gt;htmlMarkup( "http://www.wslmf.org/new/test/success.php" ); ?&gt;
      </div>
    

    I am not sure I have the above correct however.

    I don’t believe I am getting this in the correct place either

    &lt;?= $contactForm-&gt;htmlMarkup_userNotice() ?&gt;
    

    as the form submits, but doesn’t direct to the success.php page.

    Best Regards.

    #175622
    __
    Participant

    I 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.

    #175629
    Anonymous
    Inactive

    Oops, Sorry about that!

    Here’s the current page.

    #175703
    __
    Participant

    Here’s the current page.

    Your form still has action=?. Are you sure you used

    <?= $contactForm->htmlMarkup( "http://www.wslmf.org/new/test/success.php" ); ?>
    
    #175729
    Anonymous
    Inactive

    Greetings 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.

    #175730
    __
    Participant

    That’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 the action 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 );
        }
    
    #175732
    Anonymous
    Inactive

    That’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.

    #175733
    Anonymous
    Inactive

    Well, 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.

    #175748
    __
    Participant

    Well 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?

    #175751
    Anonymous
    Inactive

    Greetings 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!

    #175781
    __
    Participant

    That 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.

    #175782
    Anonymous
    Inactive

    Greetings 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.

Viewing 15 posts - 136 through 150 (of 225 total)
  • The forum ‘Other’ is closed to new topics and replies.