Jump to content

Recommended Posts

Gday,

I'm trying to add a contact form on my site using a PHP script I downloaded from the web. I have tweaked the script, but I have never used PHP before and haven't got the time to learn it yet. Could someone please help me to get this working, I have added a subject drop down field that I would like to add validation to force the user to choose one, and I'd like the subject they choose to appear in the subject field of the resulting email. Once the PHP script has run and the email has been sent I'd like the text at the bottom to appear on the original page. At the moment when i click submit it just goes to the php page and shows three lines of the error message text.

Here is the relevant code:

 

This form is placed within a static html page:

 

<form name="contactform" method="post" action="send_form_email.php" style="text-align:left;">
          <label for="first_name">First name <span class="red">*</span></label>
          <input name="first_name" type="text" value="please enter your first name" size="30" maxlength="75" onclick="document.contactform.first_name.value='';" />
          <br/>
          <label for="last_name">Last name <span class="red">*</span></label>
          <input name="last_name" type="text" value="please enter your last name" size="30" maxlength="75" onclick="document.contactform.last_name.value='';" />
          <br/>
          <label for="email" style="margin-right:32px;">Email <span class="red">*</span></label>
          <input name="email" type="text" value="please enter your email address" size="30" maxlength="75" onclick="document.contactform.email.value='';" />
          <br/>
          <label for="phone_number" style="margin-right:34px;">Phone</label>
          <input name="phone_number" type="text" value="please enter your phone number" size="30" maxlength="75" onclick="document.contactform.phone_number.value='';" />
          <br/>
          <label for="email_subject" style="margin-right:19px;">Subject <span class="red">*</span></label>
          <select name="email_subject" style="margin-bottom:10px;">
          	<option value="Choose one">Choose one</option>
            <option value="Lost my password">Lost my password</option>
            <option value="Gardening advice">Gardening advice</option>
            <option value="Order status">Order status</option>
            <option value="Web feedback">Web feedback</option>
            <option value="Customer Service">Customer Service</option>
            <option value="Product feedback">Product feedback</option>
            <option value="Other">Other</option>
          </select>
          <br/>
          <label for="comments">Queries/Comments <span class="red">*</span></label>
          <textarea rows="10" cols="50" wrap="virtual" name="comments" 
          onclick="document.contactform.comments.value='';" style="margin-bottom:5px;">Please type your query or comments here</textarea>
          <input type="submit" value="Submit" />
      </form>


and here is the seperate php script that is used:

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "johnc@diggers.com.au";
$email_subject =  $_REQUEST['email_subject'];


 function died($error) {
	// your error code can go here
	echo "We are very sorry, but there were error(s) found with the form you submitted. ";
	echo "These errors appear below.<br /><br />";
	echo $error."<br /><br />";
	echo "Please go back and fix these errors.<br /><br />";
	die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
	!isset($_POST['last_name']) ||
	!isset($_POST['email']) ||
	!isset($_POST['telephone']) ||
	!isset($_POST['email_subject']) ||
	!isset($_POST['comments'])) {
	died('We are sorry, but there appears to be a problem with the form you submitted.');		
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$telephone = $_POST['email_subject']; // required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
  	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
$string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
  	$error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!eregi($string_exp,$last_name)) {
  	$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
  	$error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  $string_exp = "^[0-9 .-]+$";
  if(!eregi($string_exp,$telephone)) {
  	$error_message .= 'The Telephone Number you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
  	died($error_message);
  }
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch within 72 hours of the next working day.

<?
}
?>

 

 

 

Any help on how to get this going would be greatly appreciated, thanks

I see a few culprits here...

1st **Block your code when posting please.

2nd You have telephone being change with the post of email subject...

3rd Your died function is being passed $error_message but its asking for $error

 

  $telephone = $_POST['telephone']; // not required
   $telephone = $_POST['email_subject']; // required 

 

Let me know if any of that helps

Thanks for that, I cleaned up the code but it still just displays the php page with the lines:

 

"We are very sorry, but there were error(s) found with the form you submitted. These errors appear below.

 

We are sorry, but there appears to be a problem with the form you submitted.

 

Please go back and fix these errors."

 

So I'm guessing it is not getting past this section:

 

	 function died($error_message) {
	// your error code can go here
	echo "We are very sorry, but there were error(s) found with the form you submitted. ";
	echo "These errors appear below.<br /><br />";
	echo $error_message."<br /><br />";
	echo "Please go back and fix these errors.<br /><br />";
	die();
}

It's getting past that...

 

Your calling your died function at

if(strlen($error_message) > 0) {
  	died($error_message);

 

You need to figure out what is actually causing the error.

 

Note, your died function is asking for ($error) not ($error_message), I would make them match...

I noticed some things in your comments, and being that the error you gets states: "We are sorry, but there appears to be a problem with the form you submitted."  Then there is one possible reason that you are getting that.  You are failing the expected data exists section of your script.

  // validation expected data exists
   if(!isset($_POST['first_name']) || //Make sure first_name is sent.
      !isset($_POST['last_name']) || //Make sure last_name is sent.
      !isset($_POST['email']) || //Make sure email is sent.
      !isset($_POST['telephone']) || //Make sure telephone is sent.
      !isset($_POST['email_subject']) || //Make sure subject is sent.
      !isset($_POST['comments'])) { //Make sure comments are sent.
      died('We are sorry, but there appears to be a problem with the form you submitted.');      
   }
   
   $first_name = $_POST['first_name']; // required
   $last_name = $_POST['last_name']; // required
   $email_from = $_POST['email']; // required
   $telephone = $_POST['telephone']; // not required  You have written the comment that telephone is NOT required, but in the block above, it is required.
   $telephone = $_POST['email_subject']; // required
   $comments = $_POST['comments']; // required

 

You should probably remove the:

!isset($_POST['telephone']) || 

In the first block of code.  As this is what requires that value to be set.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.