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? Reply To: Can someone recommend a customizable contact form, please?

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