Contact form modifications

Hi all!

I have a question about a contactform we’ve been using on our website for a few years now. I’m pretty happy with what it does now, but I would like to “extend” the capabilities of this code in order to send 2 mails in stead of one and display a “thank you”-message upon submit.

At this time, our code looks like this:


<?php
      if (!empty($_POST['Submit'])) {
      $to = "xxxx@xxxx.be";
      mail("$to", "Reactie via website", "De volgende boodschap werd u toegezonden door een gebruiker website.\
\
Naam: " . $_POST['name'] . "\
Voornaam: " . $_POST['firstname'] . "\
Firma: " . $_POST['company'] . "\
Functie: " . $_POST['function'] . "\
\
Telefoon: " . $_POST['phone'] . "\
Email: " . $_POST['email'] . "\
\
Message: " . $_POST['message'] . "\
Newz: " . $_POST['newz'], "From: Our website\\r\
"
      ."Reply-To: $email\\r\
"
      ."X-Mailer: PHP/" . phpversion());
           
      header ("Location: http://www.oursite.be/contact/index.php?id=thanks");
      exit;
      }
      ?>

This code is followed by the actual HTML form and jQuery live validation code.

So, what I would like your advice about is this:

  • In order to send an email to the visitor as well as to us, could I just dupplicate the mail(“$to”, "Reactie … part and replace the $to-variable with a $email-variable so I can define a custom mail to be sent to the visitor? If yes, can I just copy/paste and modify? Or should I take notice of certain things in order for this to work?
  • In stead of redirecting the visitor to a certain page, can I replace the header("Location: http… part by an echo/print statement without showing the form underneath on screen again?

Any help/advice on how to modify this code would be highly appreciated.

Greets,
Stef

As far as sending to multiple email addresses go you can simply separate multiple recipients by a comma in the to field.

$to = “visitoremail@email.com,ouremail@email.com”;

So I would dynamically get the visitors email and then just append your email on to the end to create a to string like the one above.

Yeah, thought of that too, but that would send the exact same email to both the visitor and me, I’d like to send out two different emails: one to us with the form field data and one tot the visitor to thank him/her for their message.

If thats the case then you will, as you described, have to duplicate the mail function (one for each email you would like to send).

As to your other question about the thank you redirect. You could do it with just a print if your form processing code and form display code are in the same file. Check to see if the form was submitted if so do one thing, if the form hasn’t been submitted then display the form.
You could do somethign like:


      if (!empty($_POST['Submit'])) {
           
           //send first email
           mail()

           //send second email
           mail()

           //Put html/print statements to render thank you message 
           echo "Thank you for submitting, you will receive a confirm email shortly"
      }else{
              //Put code to render the contact form here
              <form>
                      //more html
              </form>
      }

Looks great, seems exactly what I need! I’ll try that as soon as possible. Thanks!

Okay, so I’ve tried this with following code:


 <?php 
    if (!empty($_POST['Submit'])) {
        
    $to = "xxx@xxx.be";
    $user = $_POST['email']; 
    
    //send mail to Office
    mail("$to", "Reactie via website", "De volgende boodschap werd u toegezonden door een gebruiker van de website.\
\
Naam: " . $_POST['name'] . "\
Voornaam: " . $_POST['firstname'] . "\
Firma: " . $_POST['company'] . "\
Functie: " . $_POST['function'] . "\
\
Telefoon: " . $_POST['phone'] . "\
Email: " . $_POST['email'] . "\
\
Message: " . $_POST['message'] . "\
\
Newz: " . $_POST['newz'], "From: website\\r\
"
      ."Reply-To: $email\\r\
"
      ."X-Mailer: PHP/" . phpversion());

    //send mail to user
    mail("$user", "Reactie via website", "De volgende boodschap werd u toegezonden door een gebruiker van de website.\
\
Naam: " . $_POST['name'] . "\
Voornaam: " . $_POST['firstname'] . "\
Firma: " . $_POST['company'] . "\
Functie: " . $_POST['function'] . "\
\
Telefoon: " . $_POST['phone'] . "\
Email: " . $_POST['email'] . "\
\
Message: " . $_POST['message'] . "\
\
Newz: " . $_POST['newz'], "From: website\\r\
"
      ."Reply-To: $email\\r\
"
      ."X-Mailer: PHP/" . phpversion());

           //Put html/print statements to render thank you message 
           echo "<h1>Bedankt</h1><p>Bedankt voor uw bericht. Wij nemen zo spoedig mogelijk contact met u op.</p>";

     }else{

          //Render form
          
          echo "<h1>Contacteer ons</h1>";
          
          echo "
          <form method='post' action='".$_SERVER['PHP_self']."'>
          
          <p><label>naam</label>
          <input name='name' type='text' id='name' size='50'>
          <script type='text/javascript'>
          var name = new LiveValidation('name', { validMessage: ' ', wait: 1000 } );
          name.add( Validate.Presence, { failureMessage: 'verplicht veld' } );
          </script></p>
          
          <p><label>voornaam</label>
          <input name='firstname' type='text' id='firstname' size='50'>
          <script type='text/javascript'>
          var firstname = new LiveValidation('firstname', { validMessage: ' ', wait: 1000 } );
          firstname.add( Validate.Presence, { failureMessage: 'verplicht veld' } );
          </script></p>
          
          <p><label>firma</label>
          <input name='company' type='text' id='company' size='50'></p>
          
          <p><label>functie</label>
          <input name='function' type='text' id='function' size='50'></p>
          
          <p><label>telefoon</label>
          <input name='phone' type='text' id='phone' size='50'>
          <script type='text/javascript'>
          var phone = new LiveValidation('phone', { validMessage: ' ', wait: 1000 } );
          phone.add( Validate.Presence, { failureMessage: 'verplicht veld' } );
          phone.add( Validate.Numericality, { failureMessage: 'enkel nummers toegestaan' } );
          </script></p>
          
          <p><label>email</label>
          <input name='email' type='text' id='email' size='50'>
          <script type='text/javascript'>
          var email = new LiveValidation('email', { validMessage: ' ', wait: 1000 } );
          email.add( Validate.Presence, { failureMessage: 'verplicht veld' } );
          email.add( Validate.Email, { failureMessage: 'ongeldig adres' } );
          </script></p>
          
          <p><label>uw bericht</label>
          <textarea name='message' cols='50' rows='13' class='area' id='message'></textarea></p>
          <br/>
          
          <div style='margin-left:130px'><input name='newz' type='checkbox' id='newz' value='yes' checked> inschrijven to newsletter</div>
          
          <br/>
          <p><input type='submit' name='Submit' value=' verzenden ' class='button'></p>
      </form>";

      } 

    ?>


The form looks like it should, and live-validates using a js-file and the code within the form. When I press submit however, I get a 404 error, and neither the to-adres nor the user-email receives a message.

Something must go wrong after I press submit. Is the <form method=‘post’ action=‘“.$_SERVER[‘PHP_self’].”’> line correct?

PS: I should also mention that this script runs on a WordPress 2.8.5 install, but that should not affect this code if I’m correct.

I think you need to capitalize self in $_SERVER[‘PHP_self’] so it is $_SERVER[‘PHP_SELF’]. This should get it to at least run the script now. Let me know if the problem persists or you run into more problems.

Well, at least it did something. :wink:

When I try now, no emails arrive either. In stead of my “thank you” message it shows the default WP page index.php stating “Sorry, no posts matched your criteria.”

It could have something to do with wordpress as it appears to work fine for me running straight on my apache server.

Anyone else have any ideas?

I’ve tried installing the Exec-PHP Wordpress plugin, which allows you to embed PHP code into posts and pages, but the result is the same.

Question remains: why won’t this form submit? :frowning: