modification to PHP Contact form

I am currently trying to build a contact form for a website. I have been given a script to use that was used on another site but it has an iff statement in it to change the strEmail according to the location and requirement.

I would like to remove this as the form no longer has location or requirement and just have it post to a single email address. Below is the process php for the form.

[php]<?php
//THIS NEEDS TO BE AT THE TOP OF THE PAGE, BEFORE ANY HTML IS OUTPUT TO CLIENT
$strMessage = “”;

if ($_SERVER[‘REMOTE_ADDR’] == “5.71.114.48”) {
error_reporting(E_ALL ^ E_NOTICE);
ini_set(“display_errors”, 1);
}

//Only action if the form has been submitted
if(isset($_POST[‘submit-generic’])) {
//Validate the fields again, because not everyone has Javascript, including bots
if (isset($_POST[‘location’]) && $_POST[‘location’] !== “” &&
isset($_POST[‘requirement’]) && $_POST[‘requirement’] !== “” &&
isset($_POST[‘name’]) && $_POST[‘name’] !== “” &&
isset($_POST[‘surname’]) && $_POST[‘surname’] !== “” &&
isset($_POST[‘email’]) && $_POST[‘email’] !== “” &&
$_POST[‘surname’] !== $_POST[‘name’]) {

    switch ($_POST['location']) {
        case 'United States':
            $strEmail = "EMAIL-HERE"; break;

    }

    //Open DB
    $db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    if (mysqli_connect_error()) $db = false;

    //Prepare statement
    if ($db) {
        $insertStmt = $db->prepare("INSERT INTO enquiries (EnquiryType, EnquiryEmail, EnquiryFirstName, EnquiryLastName, EnquiryPhone, EnquiryCompany, EnquiryPosition, EnquiryRequirement, EnquiryMessage, EnquiryLocationOfInterest, EnquiryDateTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW());");
        $insertStmt->bind_param('ssssssssss', $type, $email, $firstName, $lastName, $phone, $company, $position, $requirement, $enquiry, $officeOfInterest);
    }

    //Sanitize data
    $type = "contact-form";
    $firstName = $_POST['name'];
    $lastName = $_POST['surname'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $company = $_POST['company'];
    $position = $_POST['position'];
    if ($_POST['requirement'] == "Other") {
        $requirement = $_POST['other-requirement'];
    } else {
        $requirement = $_POST['requirement'];
    }
    $enquiry = $_POST['comment'];

if ($_POST[‘location’] == “Other”) {
$officeOfInterest = $_POST[‘other-location’];
} else {
$officeOfInterest = $_POST[‘location’];
}
//Insert Data to DB
if ($db) {
$insertStmt->execute();
$insertStmt->close();
$db->close();
}

    //Send to client
    $strFrom = "EMAIL HERE";
    $strTo = $strEmail;
    $strSubject = "New contact on  from " . $_POST['name'] . " " . $_POST['surname'];
    $strBody = '
        <html>
            <body>
                <h1>New contact form</h1>
                <p>Information on form was:</p>
                <p><strong>Name</strong>: '.$_POST['name'].'</p>
                <p><strong>Surname</strong>: '.$_POST['surname'].'</p>
                <p><strong>Email</strong>: '.$_POST['email'].'</p>
                <p><strong>Phone</strong>: '.$_POST['phone'].'</p>
                <p><strong>Company</strong>: '.$_POST['company'].'</p>
                <p><strong>Position</strong>: '.$_POST['position'].'</p>
                <p><strong>Location</strong>: '.$_POST['location'].'</p>
                <p><strong>Other Location</strong>: '.$_POST['location-other'].'</p>
                <p><strong>Requirement</strong>: '.$_POST['requirement'].'</p>
				<p><strong>Other</strong>: '.$_POST['other-requirement'].'</p>
                <p><strong>Enquiry</strong>: '.$_POST['comment'].'</p>
            </body>
        </html>
    ';

    $strHeaders = 'From: '.$strFrom."\r\n".
    'Reply-To: '.$strFrom."\r\n" .
    'X-Mailer: PHP/' . phpversion() . "\r\n".
    'MIME-Version: 1.0' . "\r\n" .
    'Content-type: text/html; charset=UTF-8' . "\r\n";
    mail($strEmail, $strSubject, $strBody, $strHeaders);

    //Send confirmation to customer
    $strFrom = "EMAIL HERE";
    $strTo = $_POST['email'];
    $strSubject = "Thank you for contacting ";
    $strBody = "Thanks for getting in touch. Your message has been received and will be processed as soon as possible.";

    $strHeaders = 'From: '.$strFrom."\r\n".
    'Reply-To: '.$strFrom."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    mail($strTo, $strSubject, $strBody, $strHeaders);

    //Finally redirect
    header('Location: http:///contact-us/thank-you?contactlocation='.$_POST['location'].'?requirement='.$requirement) ;
    exit();
} else {

	//Finally redirect
    header('Location: '.$_SERVER['REQUEST_URI']. '?message=Please complete the required fields.') ;
    exit();
}

}

?>
[/php]

I only have name, surname, phone, email and comment on this new form so nothing else is needed. I also have to remove the insert into database.

any help would be appreciated!

thank you

Ok this should be fairly easy, just remove the blocks that you don’t need.
[php]
switch ($_POST[‘location’]) {
case ‘United States’:
$strEmail = “EMAIL-HERE”; break;

      }

[/php]
[php]
//Open DB
$db = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (mysqli_connect_error()) $db = false;

      //Prepare statement
      if ($db) {
          $insertStmt = $db->prepare("INSERT INTO enquiries (EnquiryType, EnquiryEmail, EnquiryFirstName, EnquiryLastName, EnquiryPhone, EnquiryCompany, EnquiryPosition, EnquiryRequirement, EnquiryMessage, EnquiryLocationOfInterest, EnquiryDateTime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW());");
          $insertStmt->bind_param('ssssssssss', $type, $email, $firstName, $lastName, $phone, $company, $position, $requirement, $enquiry, $officeOfInterest);
      }

[/php]
[php]
//Insert Data to DB
if ($db) {
$insertStmt->execute();
$insertStmt->close();
$db->close();
}
[/php]
Not sure about this next block cause it seems to deal with location in some way, so you’ll have to figure that out.
[php]
if ($_POST[‘location’] == “Other”) {
$officeOfInterest = $_POST[‘other-location’];
} else {
$officeOfInterest = $_POST[‘location’];
}
[/php]
Then change this line
[php]$strTo = $strEmail;[/php]
To this
[php]$strTo = $email;[/php]
There are some more efficient ways to do a few things in the script, but that should get you going.

I removed those bits and rather than use $email I just enetered my email directly as the $email is the users email not the email to receieve any submissions.

When I submit the form it redirects back to the current page (as it should) but I get a 404 not found :S

[php] <?php
//THIS NEEDS TO BE AT THE TOP OF THE PAGE, BEFORE ANY HTML IS OUTPUT TO CLIENT
$strMessage = “”;

if ($_SERVER[‘REMOTE_ADDR’] == “5.71.114.48”) {
error_reporting(E_ALL ^ E_NOTICE);
ini_set(“display_errors”, 1);
}

//Only action if the form has been submitted
if(isset($_POST[‘submit-generic’])) {
//Validate the fields again, because not everyone has Javascript, including bots
if (isset($_POST[‘location’]) && $_POST[‘location’] !== “” &&
isset($_POST[‘requirement’]) && $_POST[‘requirement’] !== “” &&
isset($_POST[‘name’]) && $_POST[‘name’] !== “” &&
isset($_POST[‘surname’]) && $_POST[‘surname’] !== “” &&
isset($_POST[‘email’]) && $_POST[‘email’] !== “” &&
$_POST[‘surname’] !== $_POST[‘name’]) {

      //Sanitize data
      $type = "contact-form";
      $firstName = $_POST['name'];
      $lastName = $_POST['surname'];
      $email = $_POST['email'];
      $phone = $_POST['phone'];
      $company = $_POST['company'];
      $position = $_POST['position'];
      if ($_POST['requirement'] == "Other") {
          $requirement = $_POST['other-requirement'];
      } else {
          $requirement = $_POST['requirement'];
      }
      $enquiry = $_POST['comment'];


      //Send to client
      $strFrom = "EMAIL HERE";
      $strTo = "MY EMAIL HERE";
      $strSubject = "New contact on  from " . $_POST['name'] . " " . $_POST['surname'];
      $strBody = '
          <html>
              <body>
                  <h1>New contact form</h1>
                  <p>Information on form was:</p>
                  <p><strong>Name</strong>: '.$_POST['name'].'</p>
                  <p><strong>Surname</strong>: '.$_POST['surname'].'</p>
                  <p><strong>Email</strong>: '.$_POST['email'].'</p>
                  <p><strong>Phone</strong>: '.$_POST['phone'].'</p>
                  <p><strong>Company</strong>: '.$_POST['company'].'</p>
                  <p><strong>Position</strong>: '.$_POST['position'].'</p>
                  <p><strong>Location</strong>: '.$_POST['location'].'</p>
                  <p><strong>Other Location</strong>: '.$_POST['location-other'].'</p>
                  <p><strong>Requirement</strong>: '.$_POST['requirement'].'</p>
				<p><strong>Other</strong>: '.$_POST['other-requirement'].'</p>
                  <p><strong>Enquiry</strong>: '.$_POST['comment'].'</p>
              </body>
          </html>
      ';

      $strHeaders = 'From: '.$strFrom."\r\n".
      'Reply-To: '.$strFrom."\r\n" .
      'X-Mailer: PHP/' . phpversion() . "\r\n".
      'MIME-Version: 1.0' . "\r\n" .
      'Content-type: text/html; charset=UTF-8' . "\r\n";
      mail($strEmail, $strSubject, $strBody, $strHeaders);

      //Send confirmation to customer
      $strFrom = "EMAIL HERE";
      $strTo = $_POST['email'];
      $strSubject = "Thank you for contacting ";
      $strBody = "Thanks for getting in touch. Your message has been received and will be processed as soon as possible.";

     $strHeaders = 'From: '.$strFrom."\r\n".
     'Reply-To: '.$strFrom."\r\n" .
     'X-Mailer: PHP/' . phpversion();
     mail($strTo, $strSubject, $strBody, $strHeaders);

     //Finally redirect
     header('Location: http://MY-WEBSITE-LINK/contact-us/thank-you') ;
     exit();
 } else {

	//Finally redirect
     header('Location: '.$_SERVER['REQUEST_URI']. '?message=Please complete the required fields.') ;
     exit();
 }

}

?>

[/php]

That statement doesn’t make any sense. If it directs back “as it should” then how can that also cause a 404. It’s not likely directing back where you think it is, or the page truly doesn’t exist where it’s directing too.

the page the form is on is mydomain/contact-us, when submitting the form it goes to mydomain/contact-us. The urls are 100% the same which is why I am confused.

When looking at the page source for the form the post is:

[php][/php]
Just a note I am using this within a wordpress template.

here is the code for the actual form:

[php]

<?php if ($_GET['message'] !== "") { ?>
<div class="alert">
    <?php echo $_GET['message']; ?>
</div>
<?php }; ?>

<table border="0" cellspacing="0" cellpadding="0">
    <tbody>
        <tr>
            <td class="form-wide">First Name* <input type="text" name="name" value="<?= $_POST['name'] ?>" class="form_box validate[required]" size="40" id="name-field" /></td>
        </tr>
        <tr>
            <td class="form-wide">Surname* <input type="text" name="surname" value="<?= $_POST['surname'] ?>" class="form_box validate[required]" size="40" id="surname-field" /></td>
        </tr>
        <tr>
            <td class="form-wide">Email* <input type="text" name="email" value="<?= $_POST['email'] ?>" class="form_box validate[required,custom[email]]" size="40" id="email-field" /></td>
        </tr>
        <tr>
            <td class="form-wide">Phone <input type="text" name="phone" value="<?= $_POST['phone'] ?>" class="form_box" size="40" id="phone-field" /></td>
        </tr>
        <tr>
            <td class="">Enquiry <textarea name="comment" class=" multi_box" cols="40" rows="3" id="comment-field"><?= $_POST['comment'] ?></textarea></td>
        </tr>
        
        <tr>
            <td class="form-wide" valign="top">
            <input type="submit" value="Send" class="brown-button" name="submit-generic"/></td>
        </tr>
    </tbody>
</table>

[/php]

Just try removing the text in the action of the form. That will make it submit to the same exact page it’s on.

still not working if I remove the content of action=""

According to this line, you’re sending to a /thankyou
[php]header(‘Location: http://MY-WEBSITE-LINK/contact-us/thank-you’) ;[/php]

I tried changing that to the contact page and it doesn’t help. :S the email doesn’t actually send.

you will see that the form posts back to the contact page and then redirects if the input is correct

<!-- always POST back to same page, allowing you to redirect if all input is correct -->

Well you didn’t say that it wasn’t sending the email before. You really need to be able to do basic diagnostics if you’re going to do these kinds of things.

If yo removed the ‘location’ input on the form then we forgot one of thing to remove from the processing script. Take out this line in the if()
[php]isset($_POST[‘location’]) && $_POST[‘location’] !== “” &&[/php]
Cause with the input gone, it’s returning a false on this and triggering the else{} below.

sorry about that.

I have removed everything related to feilds that are not in my actual form. Below is the most recent version that isn’t posting. It still gives me the 404 on the contact-us page.

[php] <?php
//THIS NEEDS TO BE AT THE TOP OF THE PAGE, BEFORE ANY HTML IS OUTPUT TO CLIENT
$strMessage = “”;

if ($_SERVER[‘REMOTE_ADDR’] == “5.71.114.48”) {
error_reporting(E_ALL ^ E_NOTICE);
ini_set(“display_errors”, 1);
}

//Only action if the form has been submitted
if(isset($_POST[‘submit-generic’])) {
//Validate the fields again, because not everyone has Javascript, including bots
if ( isset($_POST[‘name’]) && $_POST[‘name’] !== “” &&
isset($_POST[‘surname’]) && $_POST[‘surname’] !== “” &&
isset($_POST[‘email’]) && $_POST[‘email’] !== “” &&
$_POST[‘surname’] !== $_POST[‘name’]) {

      //Sanitize data
      $type = "contact-form";
      $firstName = $_POST['name'];
      $lastName = $_POST['surname'];
      $email = $_POST['email'];
      $phone = $_POST['phone'];
      $enquiry = $_POST['comment'];


      //Send to client
      $strFrom = "EMAIL HERE";
      $strTo = "[email protected]";
      $strSubject = "New contact on  from " . $_POST['name'] . " " . $_POST['surname'];
      $strBody = '
          <html>
              <body>
                  <h1>New contact form</h1>
                  <p>Information on form was:</p>
                  <p><strong>Name</strong>: '.$_POST['name'].'</p>
                  <p><strong>Surname</strong>: '.$_POST['surname'].'</p>
                  <p><strong>Email</strong>: '.$_POST['email'].'</p>
                  <p><strong>Phone</strong>: '.$_POST['phone'].'</p>
                  
                  <p><strong>Enquiry</strong>: '.$_POST['comment'].'</p>
              </body>
          </html>
      ';

      $strHeaders = 'From: '.$strFrom."\r\n".
      'Reply-To: '.$strFrom."\r\n" .
      'X-Mailer: PHP/' . phpversion() . "\r\n".
      'MIME-Version: 1.0' . "\r\n" .
      'Content-type: text/html; charset=UTF-8' . "\r\n";
      mail($strEmail, $strSubject, $strBody, $strHeaders);

      //Send confirmation to customer
      $strFrom = "EMAIL HERE";
      $strTo = $_POST['email'];
      $strSubject = "Thank you for contacting ";
      $strBody = "Thanks for getting in touch. Your message has been received and will be processed as soon as possible.";

     $strHeaders = 'From: '.$strFrom."\r\n".
     'Reply-To: '.$strFrom."\r\n" .
     'X-Mailer: PHP/' . phpversion();
     mail($strTo, $strSubject, $strBody, $strHeaders);

     //Finally redirect
     header('Location: http://DOMAIN/contact-us') ;
     exit();
 } else {

	//Finally redirect
     header('Location: '.$_SERVER['REQUEST_URI']. '?message=Please complete the required fields.') ;
     exit();
 }

}

?>

[/php]

also to note I have included the file using the below include that is right under the html tag

[php] <?php include('generic-top.php'); ?>[/php]

I guess at this point I am lost as to what to do without being able to see it live and test things myself. You may want ask this on a worpress forum since they will know more about how the whole system works.

Sponsor our Newsletter | Privacy Policy | Terms of Service