I am trying to make the PHP code to work, but haven’t been able to make it work. So can someone please have a look at my code below, and tell me how I can fix it? I know that the Submit button is named submitbtn. Thanks in advance.
if (isset($_POST['submitbtn'])) {
$to = "mail@domainname.com";
$subject = "Subject here";
$message = "Full Name: $fullname
Phone Number: $phone
Email Address: $email
Project Details: $details";
$email = $_POST['email'];
mail($to, $subject, $message, $email);
header("Location: sent.htm");
exit;
}
Well my php email code is working now, but when I click on the submit button, it doesn’t redirect to the sent.htm. It just makes the form disappear all together, leaving the rest of the web page displaying on the page. I do receive the email which was generated from the form, but it just doesn’t redirect to sent.htm. Can someone please tell me why this could be happening? I have changed my code to what is below:
PHP Code:
if (isset($_POST['submitbtn'])){
$to = "mail@mydomainname.com";
$subject = "Subject here";
$message = 'Full Name: ' . $fullname . " ";
$message .= 'Phone Number: ' . $phone . " ";
$message .= 'Email Address: ' . $email . " ";
$message .= 'Project Details: ' . $details;
$email = $_POST['email'];
$headers = 'MIME-Version: 1.0' . "\\r\
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\\r\
";
$headers .= 'From: <' . $email . '>';
if (mail($to, $subject, $message, $headers)) {
header('Location: sent.htm');
}
}
HTML:
<form id="projectDetails" name="projectDetails" action="<?php echo $PHP_SELF; ?>" method="post">
<div class="formElements">
<label id="fullName">Full Name:</label>
<input name="fullname" type="text" size="51" />
<label id="phoneNumber">Phone Number:</label>
<input name="phone" type="text" size="51" />
<label id="emailAddress">Email Address:</label>
<input name="email" type="text" size="51" />
<label id="projectDetails">Project Details:</label>
<textarea cols="40" name="details" rows="6"></textarea>
<input name="submitbtn" id="submitbtn" type="submit" value="Submit" />
</div>
</form>
Another thing is the how do I make the email, which was generated from the form, appear like this:
Full Name: Jo Blow
Phone Number: (02) 6859 2222
Email Address: mail@yourdomainname.com
Project Details: Unknown as yet.
At the moment it appears like this (all on one line):
Full Name: Jo Blow Phone Number: (02) 6859 2222 Email Address: mail@yourdomainname.com Project Details: Unknown as yet.
HI
First of all you if you are testing locally you need to have a local mail server. you can download local email servers applications.
If you are using on remote server then this should send email to the one you pass from the form.
<?php
if(isset($_POST['submitbtn'])){
$subject = "Subject here";
$message = "your message here";
$email = $_POST['email'];
$headers = 'MIME-Version: 1.0' . "\\r\
";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\\r\
";
$headers .= 'From: your name <yourname@yourdomain.com>' . "\\r\
";
if(mail($to, $subject, $message,$headers )){
header("Location: sent.htm");
exit;
}
}
?>
<form id="form1" name="form1" method="post" action="">
<input type="text" name="email" id="email" />
<br />
<input type="submit" name="submitbtn" id="submitbtn" value="Submit" />
</form>
Note: if you dont have mail server setup then you will get this type of error:
Warning: mail() [function.mail]:Failed to connect to mailserver.
that means the code is working properly.