简体   繁体   中英

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. john.smith@abc.com"> </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">
    </div>
</fieldset>
<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>
        </select>
    </div>
</fieldset>
<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>
    <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>
    <div class="form-row form-button column">
        <button type="submit" class="button-orange">Send Message</button>
    </div>
    <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>
    </div>
</fieldset>
</form>

And the PHP:

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

$mail_to = 'email@example.com';
 $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';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
    window.location = 'contact.html';
</script>
<?php
}?>

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("someone@example.com","My subject",$msg); check the response and if it actually delivers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM