Zoned Zoned - 1 year ago 109
PHP Question

PHP Email Form with Redirect

first let me start of that i do not know that much about php and html. I would like to create a php form with a redirect to the home page. Anytime i fill out the contact form it does not send out an email nor does it do anything on the page.

Here is the html:

<form action="contact-form.php" method="post" class="contact-form" id="contact-form">
<fieldset class="form-rows row">
<div class="form-row column medium-6">
<label for="contact-name">Your name</label>
<input type="text" name="name" id="contact-name" placeholder="e.g. John Smith"> </div>
<div class="form-row column medium-6">
<label for="contact-email">Email address</label>
<input type="email" name="email" id="contact-email" placeholder="e.g."> </div>
<div class="form-row column medium-12 half">
<label for="contact-phone">Phone number
<br><small>We promise we will only use your phone number to contact you regarding this message.</small></label>
<input type="tel" name="phone" id="contact-phone" class="half" placeholder="e.g. 123 456 7890">
<fieldset class="for-rows row">
<div class="form-row column medium-12">
<label for="contact-subject">Message subject</label>
<select name="subject" id="contact-subject">
<option value="">Please select one</option>
<option value="1">General enquiry</option>
<option value="2">I'd like to say thank you!</option>
<option value="2">Something wasn't right with my order!</option>
<fieldset class="form-rows row">
<div class="form-row column">
<label for="contact-message">Your message</label>
<textarea name="message" id="contact-message" rows="8" placeholder="Enter message here &hellip;"></textarea>
<div class="form-row column">
<input type="checkbox" name="newsletter" id="contact-newsletter" placeholder="">
<label for="contact-newsletter">I would like to receive information about news, promotions, customer surveys, competitions, exclusive offers, events.</label>
<div class="form-row form-button column">
<button type="submit" class="button-orange">Send Message</button>
<div class="column">
<h3>Your information</h3>
<p>We will use your information for dealing with your request and, if you agree, to contact you with information about news, promotions, customer surveys, competitions, exclusive offers, events and other information we think may be of interest to you. We will not share your information for marketing purposes with any third party.</p>

And the PHP:

$field_name = $_POST['name'];
$field_email = $_POST['email'];
$field_phone = $_POST['phone'];
$field_subject = $_POST['subject'];
$field_message = $_POST['message'];

$mail_to = '';
$subject = $field_subject;

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

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

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

if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
window.location = 'index.html';
else { ?>
<script language="javascript" type="text/javascript">
window.location = 'contact.html';

Answer Source

your redirection is senseless, you should use php header like this:

if ($mail_status) { 
    header('Location: index.html');
else {
    header('Location: contact.html');

the mail function looks ok, I would recomend you use phpmailer because the mails need proper headers to be properly accepted.

You could do var_dump($_POST); die(); to see if you're recieving the _POST properly.

also mail() = true doesnt mean it reached its destination

Your problem could be server more than php

Are you doing this in local server? This wont work, use test mail server for debugging purposes.

Are you doing this in a host? Check php.ini permissions, try with a simple script mail("","My subject",$msg); check the response and if it actually delivers.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download