This contact.php will process the data and send it to my email. I want to achieve this effect, when user submit the form correctly, it will display a alert, and then redirect the page to : thankyou.html.
When there is some error, it will display another alert, and then redirect the page to error.html.

Whether below code can get this done? Thank you for your help or suggestion.

<?php
$field_firstname = $_POST['cf_firstname'];
$field_lastname = $_POST['cf_lastname'];
$field_emailaddress= $_POST['cf_emailaddress'];
$field_telepohone = $_POST['cf_telephone'];
$field_country = $_POST['cf_country'];
$field_business = $_POST['cf_business'];
$field_comments = $_POST['cf_comments'];

$mail_to = 'someone@somewhere.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$cf_email."\r\n";
$headers .= 'Reply-To: '.$cf_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
	<script language="javascript" type="text/javascript">
		alert('Thank you for the message. We will contact you shortly.');
	
		setTimeout("window.location= 'thankyou.html', 1250);
	</script>
<?php
}
else { ?>
	<script language="javascript" type="text/javascript">
		alert('Message failed. Please check the items required');
		
		setTimeout("window.location= 'error.html', 1250);
	</script>
<?php
}
?>

Recommended Answers

All 9 Replies

Why You can't try this code?

if ($mail_status) 
{ 
header('location:thankyou.html');
}
else 
{ 
header('location:error.html');
}
?>

and you can use your scripts in thankyou.html and error.htm.

Well, the variables you're using in these lines of code

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

have not been defined in the code that precedes it (for as far as I can see), and therefore will be null (if what I see here is all your code).

Well, the variables you're using in these lines of code

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

have not been defined in the code that precedes it (for as far as I can see), and therefore will be null (if what I see here is all your code).

thank you minitauros for your reminder, I do made mistake obviously, I copied this code from website and do some change but it seems you are correct that I should modify it more. pls see below.


$body_message = 'From: '.$field_firstname."\n";
$body_message = 'Email: '.$field_emailaddress."\n";
$body_message = 'Message: '.$field_comments;

how should I add lastname, country, and business to above code?

I'd probably re-write it as follows,

// This merges the firstname and lastname into one variable
$field_name = $_POST['cf_firstname']." ".$_POST['cf_lastname'];

// Create e-mail message body
$body_message  = 'From:   '.$field_name.'\n';
$body_message .= 'E-mail: '.$_POST['cf_emailaddress'].'\n';
$body_message .= 'Message: \n'.$_POST['cf_emailaddress'].'\n';
$body_message .=  $_POST['cf_telephone'].'\n';
$body_message .=  $_POST['cf_country'].'\n';
$body_message .=  $_POST['cf_business'].'\n';
$body_message .=  $_POST['cf_comments'];

$headers = 'From: '.$_POST['cf_emailaddress'].'\r\n';
$headers .= 'Reply-To: '.$_POST['cf_emailaddress'].'\r\n';

$mail_to = 'someone@somewhere.com';
$subject = 'Message from a site visitor '.$field_name;
 
$mail_status = mail($mail_to, $subject, $body_message, $headers);

There are some things that you may want to do like sanitize the input to protect yourself against malicious users.

Cheers,
Mark

$body_message = 'From: '.$field_firstname."\n";
$body_message .= 'Last Name: '.$field_lastname."\n";
$body_message .= 'Email: '.$field_emailaddress."\n";
$body_message .= 'Message: '.$field_comments."\n";
$body_message .= 'Country: '.$field_country."\n";
$body_message .= 'Business: '.$field_business;

Than you Markdh and Karthik, it's really appreciated, I will try again as what you suggest, and see if it works this time.

hello everyone, I have used validate form action, but it seems the behavior is not fuctioned well at all, user could submit the blank form too. and in addition, user email could not show properly. now I attached contact.html and contact.php. pls help me find the root cause if you could. it's really alittle complicated to me. thank you all.

code of contact.html

<form action="contact.php" method="post" name="form1" target="_blank" class="email" id="form1" onsubmit="MM_validateForm('firstname','','R','lastname','','R','emailaddress','','NisEmail','comments','','R');return document.MM_returnValue">
      <p>If you need more information about our inspection, sourcing, auditing, and verification service, <br />
        <br />
        pls contact below address or just fill in the form below, our customer service will get you back <br />
        <br />
      as soon as possible. thank you for considering Zooqc!</p>
      <p>First Name: 
        &nbsp;&nbsp;&nbsp;
        <label for="firstname"></label>
        &nbsp;&nbsp;&nbsp;
        <input type="text" name="firstname" id="firstname" />
        <br />
        <br />
        Lsat Name: 
        <label for="lastname"></label>
        &nbsp;&nbsp;&nbsp;
        &nbsp;&nbsp;&nbsp;
        <input type="text" name="lastname" id="lastname" />
        <br />
        <br />
        Email Address:
        &nbsp;&nbsp;
        <label for="emailaddress"></label>
        <input type="text" name="emailaddress" id="emailaddress" />
        <br />
        <br />
        Telephone:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<label for="telephone2"></label>
        <input type="text" name="telephone" id="telephone" />
        <label for="TEL"></label>
        <br />
        <br />
        Country:
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <label for="country"></label>
        <select name="country" id="country">
          <option>Australia</option>
          <option>Austria</option>
          
          <option>other</option>
        </select>
        <br />
        <br />
        Business:
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <label for="business"></label>
        <select name="business" id="business">
          <option>wholesale</option>
          <option>retail</option>
          <option>industrial</option>
          <option>services</option>
          <option>distributor</option>
          <option>manufacturer</option>
          <option>importer</option>
          <option>other</option>
        </select>
        <label for="telephone"></label>
        <label for="FAX"></label>
        <br />
        <br />
        Comments:<br />
        <br />
        <label for="comments"></label>
        <textarea name="comments" id="comments" cols="45" rows="5"></textarea>
        <br />
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input name="submit" type="submit" id="submit" onclick="MM_validateForm('firstname','','R','lastname','','R','emailaddress','','RisEmail','comments','','R');return document.MM_returnValue" value="Submit" />
        <br />
      </p>
    </form>

code of contact.php

<?php
$field_firstname = $_POST['cf_firstname'];
$field_lastname = $_POST['cf_lastname'];
$field_emailaddress= $_POST['cf_emailaddress'];
$field_telepohone = $_POST['cf_telephone'];
$field_country = $_POST['cf_country'];
$field_business = $_POST['cf_business'];
$field_comments = $_POST['cf_comments'];

$mail_to = 'info@zooqc.com';
$subject = 'Message from a site visitor '.$field_name;

$body_message = 'From: '.$field_firstname."\n";
$body_message .= 'From: '.$field_lastname."\n";
$body_message .= 'E-mail: '.$field_emailaddress."\n";
$body_message .= 'Message: '.$field_telephone;
$body_message .= 'Message: '.$field_country;
$body_message .= 'Message: '.$field_business;
$body_message .= 'Message: '.$field_comments."\n";

$headers = 'From: '.$cf_email."\r\n";
$headers .= 'Reply-To: '.$cf_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
	<script language="javascript" type="text/javascript">
		alert('Thank you for the message. We will contact you shortly.');
	
		<!--
window.location.href = "http://www.zooqc.com/thankyou.html";
//-->

	</script>
<?php
}
else { ?>
	<script language="javascript" type="text/javascript">
		alert('Message failed. Please check the items required');
		
		<!--
window.location.href = "http://www.zooqc.com/contact.html";
//-->

	</script>
<?php
}
?>

Have you checked that SMTP server is configured correctly????
If not then configure it.....

why I keep getting message that show the sender name: rnReply-To: rn@yourhostingaccount.com.

is this normal or not? why shouldn't it be the user's email. does the validate form works? thank you if you could have some suggestion.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.