0

我试图让表单不提交到 send_email.php,而是留在同一页面上。另外,我希望错误显示在输入框下。(如果我了解如何保持页面不刷新,我想我可以犯错误)

我一直在阅读我应该使用 AJAX 的 stackoverflow,但我不知道如何开始。

形式:

<form name="contactform" method="post" action="send_email.php">
<table>
<tr>
    <td><label for="name">Name:</label></td>
    <td><input type="text" name="name" /></td>
    </tr>
    <tr>
    <td><label for="email">Email:</label></td>
    <td><input type="text" name="email" /></td>
    </tr>
    <tr>
    <td><label for="phone">Phone:</label></td>
    <td><input type="text" name="phone" /></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input class="button" type="submit" value="Submit" /></td>
    </tr>
    </table>
    </form>

发送电子邮件.php:

<?php
if(isset($_POST['email'])) {

$email_to = "example@example.com";
$email_subject = "Form Subject";


function died($error) {
    echo $error."<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['phone']))
 {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // required


$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'Enter a valid email.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'Enter a valid name.<br />';
}
if(strlen($telephone) != NULL || strlen($telephone) != "") {
$error_message .= 'Enter valid phone.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";



// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>


<b>Thank you!</b>. 

<?php
}
?>
4

3 回答 3

1
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<SCRIPT TYPE="text/javascript">

$(document).ready(function(){

$("form").submit(function() {

var name = $('input[name="name"]').val();
var email = $('input[name="email"]').val();
var phone = $('input[name="phone"]').val();


$.post("send_email.php", { "name": name,"email": email,"phone": phone},
 function(data){

alert( data.response + data.email);

 }, "json");
return false;
});
});
</SCRIPT>
</head>
<body>
<form name="contactform" method="post" action="">
<table>
<tr>
    <td><label for="name">Name:</label></td>
    <td><input type="text" name="name" /></td>
    </tr>
    <tr>
    <td><label for="email">Email:</label></td>
    <td><input type="text" name="email" /></td>
    </tr>
    <tr>
    <td><label for="phone">Phone:</label></td>
    <td><input type="text" name="phone" /></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input class="button" type="submit" value="Submit" /></td>
    </tr>
    </table>
    </form>
</body>
</html>

==================================================== =========================

send_email.php 看起来像这样

<?php

$email_to = $_POST['email'];
$email_subject = "Form Subject";


$first_name = $_POST['name']; // required
$email_from = "example@example.com"; // required
$telephone = $_POST['phone']; // required


$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'Enter a valid email.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'Enter a valid name.<br />';
}
if(strlen($telephone) != NULL || strlen($telephone) != "") {
$error_message .= 'Enter valid phone.<br />';
}

$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message = "Name:" . $_POST['name'] . " Mail:".$_POST['email'] ." Phone:". $_POST['phone'];




// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if(@mail($email_to, $email_subject, $email_message, $headers))
{
echo '{ "response": "SENT", "email": "'.$_POST['email'].'"}'; 
}else{
echo '{ "response": "ERROR", "email": "'.$_POST['email'].'"}'; 
}



?>

您可以使用 jquery 验证您的表单,我从您的 php 中删除了一些不必要的验证行,因此当您确定您的代码有效并且 mail_to 地址未指向表单邮件地址时,您可以稍后再次添加。

我检查了上面的代码,它可以工作。祝你好运。

注意:在我的服务器中 if(@mail($email_to, $email_subject, $email_message, $headers)) 总是返回 true,即使我使用空白作为电子邮件地址

于 2012-11-02T08:04:21.127 回答
0

$.post('send_email.php', $('#theform').serialize()) //其中form是你要提交的表单的id..

您可以在以下链接中找到更多可用选项

http://api.jquery.com/jQuery.post/

于 2012-11-02T07:13:48.983 回答
0

你可以试试下面的代码。在这里,我们没有使用 ajax,而是使用会话。

表单.php

<?php
session_start();
?>

<form name="contactform" method="post" action="send_email.php">
    <table>
<tr>
    <td><label for="name">Name:</label></td>
    <td><input type="text" name="name" /></td>
    </tr>    
    <tr>
    <td colspan="2"><?php echo $_SESSION[lmsg_name]; ?></td>
    </tr>

<tr>
<td><label for="email">Email:</label></td>
<td><input type="text" name="email" /></td>
</tr>
 <tr>
<td colspan="2"><?php echo $_SESSION[lmsg_email]; ?></td>
</tr>

<tr>
<td><label for="phone">Phone:</label></td>
<td><input type="text" name="phone" /></td>
</tr>
 <tr>
<td colspan="2"><?php echo $_SESSION[lmsg_phone]; ?></td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input class="button" type="submit" value="Submit" /></td>
</tr>
</table>
    </form>
<?php
 $_SESSION[lmsg_name] ='';
 $_SESSION[lmsg_email] ='';
 $_SESSION[lmsg_phone] =''; 
?>

发送电子邮件.php:

<?php
session_start();

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

$email_to = "example@example.com";
$email_subject = "Form Subject";


function died($error) {
    echo $error."<br /><br />";
    die();
}

// validation expected data exists
if(!isset($_POST['name']) ||
    !isset($_POST['email']) ||
    !isset($_POST['phone']))
 {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
}

$first_name = $_POST['name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['phone']; // required



$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message = 'Enter a valid email.<br />';
$_SESSION[lmsg_email] = $error_message;
header("Location:form.php");
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message = 'Enter a valid name.<br />';
$_SESSION[lmsg_name] = $error_message;
header("Location:form.php");
}
if(strlen($telephone) != NULL || strlen($telephone) == "") {
$error_message = 'Enter valid phone.<br />';
$_SESSION[lmsg_phone] = $error_message;
header("Location:form.php");
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "Name: ".clean_string($first_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";



// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>


<b>Thank you!</b>. 

<?php
}
?>
于 2012-11-02T07:20:26.220 回答