motomoto
asked on
How do i send html form data via email using php?
I created a simple html form and a php file but each time I submit the form and i open the email , its empty. Asa result i have block my if statement to that checks to make sure certain variable are not empty because even i enter data in the form it was coming up empty hence redirecting to the form again. can someone help please?
HTMLl form :
<html>
FORM action="scripts/contact_form.php" method="post" enctype="text/plain" name="contactform" id="Contactform">
<tr ><td align="right">Reason for contact</td> <td align="left">
<select name="reason[]" multiple="yes">
<option value="passport"> Passport</option>
<option value="trade">Trade</option>
<option value="tourism">Tourism</option>
<option value="visa">Visa</option>
</select>
</td>
</tr>
<tr ><td align="right"><span class="style2">*</span>Full Name</td>
<td align="left"><input type="text" name="fname1" size="52" value=""></td></tr>
<tr >
<td align="right">Address</td>
<td align="left"><input type="text" name="address" size ="52" ></td></tr>
<tr >
<td align="right"><span class="style2">*</span>Phone</td>
<td align="left"><input type="text" name="phone" size ="52"></td></tr>
<tr ><td align="right">Fax</td> <td align="left"><input type="text" name="fax" size ="52"></td></tr>
<tr >
<td align="right"><span class="style2">*</span>Email</td>
<td align="left"><input type="text" name="semail" size ="52"></td></tr>
<tr >
<td align="right"><span class="style2">*</span>Comments</td>
<td align="left"><textarea type="text" name="comments" cols=5 rows=4 ></textarea></td></tr>
<tr> <td colspan="2" align="center"><input type="submit" value="Submit" /> <input type="reset" /></td></tr>
</FORM>
</table>
</html>
------------------------------------------------------------------------
PHP file:
<?php
// Makes sure required fields are not empty - currently blocked
/*
if (($_POST[fname] == "") || ($_POST[semail] == "") || ($_POST[comments] == "")) {
header("Location: ../tzecontact.html");
exit;
}
*/
$msg = "Contact Request from Website\n";
$msg .= "Sender's Name: $_POST[fname1]\n";
$msg .= "Sender's Address: $_POST[address]\n";
$msg .= "Sender's Phone: $_POST[phone]\n";
$msg .= "Sender's Fax: $_POST[fax]\n";
$msg .= "Sender's Email: $_POST[semail]\n";
$msg .= "Reason: $_POST[reason]\n";
$msg .= "Message: $_POST[comments]\n";
$to = "jonny007@hotmail.com";
$subject = "Contact Request from Website";
$mailheaders = "From: Tan <info@tan.org>\n";
$mailheaders .= "Reply to: $_POST[$semail]\n";
mail ($to, $subject, $msg, $mailheaders);
header( "Location: ../thankyou.html" );
?>
----------------------------------------
Email results:
Contact Request from Website
From: Tan (info@tan.org)
Sent: Fri 4/17/09 1:42 AM
To: jonny007@mail.com
Reply to:
Contact Request from Website
Sender's Name:
Sender's Address:
Sender's Phone:
Sender's Fax:
Sender's Email:
Reason:
Message:
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
also, I always declare my variables first:
$fname1 = $_POST['fname1'];
$address = $_POST['address'];
then you can write:
$msg .= "Sender's Name: $fname1\n";
$msg .= "Sender's Address: $address\n";
I'm not sure if it is a requirement but my form template works great this way.
$fname1 = $_POST['fname1'];
$address = $_POST['address'];
then you can write:
$msg .= "Sender's Name: $fname1\n";
$msg .= "Sender's Address: $address\n";
I'm not sure if it is a requirement but my form template works great this way.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Most of the solutions provided were easy to understand but one was a bit hard to get. Overall the help provided was great, Thanks!
ASKER
Thanks Guys for all your help. I really appreciate it.
I got my script working now, in case you are interested of how it looks see below....
I got my script working now, in case you are interested of how it looks see below....
<?php
// Makes sure required fields are not empty
if (($_POST[fname] == "") || ($_POST[phone] == "") || ($_POST[semail] == "") || ($_POST[comments] == "")) {
header("Location: ../requiredcontact.html");
exit;
}
// Checked "reason" presence and sets to $value
if (!empty($_POST["reason"])) {
foreach ($_POST["reason"] as $value ) {
//Set email body variables
$msg = "Name: " . $_POST["fname"] . "\n";
$msg .= "Address: " . $_POST["address"] . "\n";
$msg .= "Phone: " . $_POST["phone"] . "\n";
$msg .= "Fax: " . $_POST["fax"] . "\n";
$msg .= "Email: " . $_POST["semail"] . "\n\n";
$msg .= "Reason: " . $value . "\n\n";
$msg .= "Message: " . $_POST["comments"] . "\n";
$subject = "Contact Request from: " . $_POST["fname"] . " ";
$mailheaders = "From: Tan-USA <info@tan.org>\n";
$mailheaders .= "Reply to: " . $_POST["semail"] . "\n";
//Set delivery email variables
$passport_email = "passport@tan.org";
$trade_email = "trade@tan.org";
$tourism_email = "tourism@tan.org";
$visa_email = "visa@tan.org";
//Delivery conditions driven from "reason"
if ($value == "Passport") {
mail ($passport_email, $subject, $msg, $mailheaders);
}
elseif ($value == "Trade") {
mail ($trade_email, $subject, $msg, $mailheaders);
}
elseif ($value == "Tourism") {
mail ($tourism_email, $subject, $msg, $mailheaders);
}
elseif ($value == "Visa") {
mail ($visa_email, $subject, $msg, $mailheaders);
exit;
}
//Redirect user to thank you page after successful delivery
if (($value == "Passport") || ($value == "Trade") || ($value == "Visa") || ($value == "Tourism")) {
header( "Location: ../thankyou.html" );
exit;
}
}
exit;
}
?>
ASKER
Is there something else I'm missing?
btw why the extra dots and "" ?
Open in new window