首页 > 解决方案 > Email contact form with response in the same page

问题描述

i've this email contact form that works well but open me the response in a blank empty page. I would the response in the same page. Can anyone help me? Thank you in advance.

<?php

// Site Info
$site_name  = 'xxx';
$site_email = 'info@sitename.com';

if(isset($_POST['contact_email'])){

	$contact_name    = $_POST['contact_name'];
	$contact_phone   = $_POST['contact_phone'];
	$contact_email   = $_POST['contact_email'];
	$contact_subject = $_POST['contact_subject'];
	$contact_message = $_POST['contact_message'];
    $from_email =  $contact_email;
	$contact_name         = "Name: $contact_name <br />";
	$contact_email        = "Email:  $contact_email <br />";
	$contact_phone         = "Phone Number: $contact_phone <br />";
	$contact_subject       = "Subject: $contact_subject <br />";
	$contact_message       = "Message: $contact_message <br />";

	$to = $site_email;
	$subject = "You have a new email from ".$site_name;
	$header  = 'MIME-Version: 1.0' . "\r\n";
	$header .= 'Content-type: text/html; charset=utf-8' . "\r\n";
	$header .= 'From:'.$from_email. " \r\n";
	$message = "
		You have a new message! <br />
		$contact_name
		$contact_email
		$contact_phone
		$contact_subject
		$contact_message
	";

	// Send Mail
	if(@mail($to,$subject,$message,$header)) {
		$send = true;
	} else {
		$send = false;
	}

	if(isset($_POST['ajax'])){
		if($send)
			echo 'success';
		else
			echo 'error';
	}
}
HTML
<form id="contact_form" name="contact_form" method="post" action="page_contact_ajax.php" >
                                    <div class="form-row field_text">
                                     
                                        <label for="contact_name">Your Name </label>
                                        <em>(required)</em><br>
                                        <input id="contact_name" class="input_text required" type="text" value="" name ="contact_name">
                                                                      </div>
                                    <div class="form-row field_text">
                                        <label for="contact_phone">Your Phone Number </label><em>(optional)</em><br>
                                        <input id="contact_phone" class="input_text" type="text" value="" name ="contact_phone">
                                    </div>

                                    <div class="form-row field_text">
                                        <label for="contact_email">Your E-Mail Address </label><em>(required)</em><br>
                                        <input id="contact_email" class="input_text required" type="text" value="" name ="contact_email">
                                    </div>
                                    <div class="form-row field_text">
                                        <label for="contact_subject">Subject </label><em>(required)</em><br>
                                        <input id="contact_subject" class="input_text required" type="text" value="" name ="contact_subject">
                                    </div>
                                    <div class="form-row field_textarea">
                                        <label for="contact_message">Message: </label><br>
                                        <textarea id="contact_message" class="input_textarea" type="text" value="" name ="contact_message"></textarea>
                                    </div>
                                    <div class="form-row field_submit">
                                        <input type="submit" value="Submit Now" id="contact_submit" class="ui button colored">
                                        <span class="loading hide"><img src="assets/images/preloader.gif"></span>
                                    </div>
                                    <div class="form-row notice_bar">
                                        <p class="notice notice_ok hide">Thank you for contacting us. We will get back to you as soon as possible.</p>
                                        <p class="notice notice_error hide">Due to an unknown error, your form was not submitted. Please resubmit it or try later.</p>
                                    </div>
                                </form> 

HTML

Your Name (required)
Your Phone Number (optional)
Your E-Mail Address (required)
Subject (required)
Message:
Thank you for contacting us. We will get back to you as soon as possible.

Due to an unknown error, your form was not submitted. Please resubmit it or try later.

标签: phphtmlformsemailresponse

解决方案


PHP 手册说在某些情况下替换\r\n为有效。\n

Yon 可以从中删除@@mail(...)使用error_get_last()以查看mail. 看到这个答案


推荐阅读