2012-11-02 104 views
0

我想讓表單不要提交給send_email.php,而是留在同一頁面上。另外,我希望在輸入框下顯示錯誤。 (我想我能做到的錯誤,如果我知道如何從刷新保持頁)驗證表單並提交到相同的頁面

我一直在讀遍計算器,我應該使用AJAX它,但我不知道如何下手。

形式:

<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> 

send_email.php:

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

$email_to = "[email protected]"; 
$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._%-][email protected][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 
} 
?> 

回答

0

$。員額( 'send_email.php',$( '#theform')序列化()) //其中表單是您要提交的表單的編號..

您可以在以下鏈接找到更多選項

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

0

您可以試試下面的代碼。這裏我們不使用ajax,而是使用會話。

form.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] =''; 
?> 

send_email.php:

<?php 
session_start(); 

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

$email_to = "[email protected]"; 
$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._%-][email protected][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 
} 
?> 
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 = "[email protected]"; // required 
$telephone = $_POST['phone']; // required 


$error_message = ""; 
$email_exp = '/^[A-Za-z0-9._%-][email protected][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地址是沒有指向表單郵件地址。

我檢查了上面的代碼,它的工作原理。祝你好運。

注:在我的服務器,如果(@mail($ EMAIL_TO,$ EMAIL_SUBJECT,$ email_message,$頭))總是返回true,即使我用空白電子郵件地址

+0

工程在Opera,無法正常工作的資源管理器或火狐,電子郵件發送,但在Chrome瀏覽器和Safari瀏覽器空白字段...有什麼想法呢? – Travis

+0

我更改了php文件,您可以使用修改來修復空白郵件。 – Kad

+0

Travis現在可以在Firefox瀏覽器中使用Chrome瀏覽器和其他瀏覽器,使用上面的修改過的文件。 – Kad