我正在使用Google搜索中的联系表单。我似乎无法弄清楚如何通过Ajax将下拉列表(select)中的值发送到联系表单的php文件。
请查看我的jsfiddle。 http://jsfiddle.net/CBSeZ/
<?php
if($_POST)
{
//check if its an ajax request, exit if not
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die();
}
$to_Email = "email@email.com";
$subject = 'TimeKeeperPro Email';
//check $_POST vars are set, exit if any missing
if(!isset($_POST["userName"]) || !isset($_POST["userEmail"]) || !isset($_POST["userSubject"]) || !isset($_POST["userMessage"]))
{
die();
}
//Sanitize input data using PHP filter_var().
$user_Name = filter_var($_POST["userName"], FILTER_SANITIZE_STRING);
$user_Email = filter_var($_POST["userEmail"], FILTER_SANITIZE_EMAIL);
$user_Subject = filter_var($_POST["userSubject"], FILTER_SANITIZE_STRING);
$user_Message = filter_var($_POST["userMessage"], FILTER_SANITIZE_STRING);
//additional php validation
if(strlen($user_Name)<1) // If length is less than 4 it will throw an HTTP error.
{
header('HTTP/1.1 500 Your name is too short or empty!');
exit();
}
if(!filter_var($user_Email, FILTER_VALIDATE_EMAIL)) //email validation
{
header('HTTP/1.1 500 Please enter a valid email!');
exit();
}
if(strlen($user_Message)<5) //check emtpy message
{
header('HTTP/1.1 500 Your message is too short! Please enter something.');
exit();
}
//proceed with PHP email.
$headers = 'From: '.$user_Email.'' . "\r\n" .
'Reply-To: '.$user_Email.'' . "\r\n" .
'MIME-Version: 1.0' . "\r\n" .
'Content-type: text/html; charset=iso-8859-1' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
@$sentMail = mail($to_Email, $subject, $user_Message .' -'.$user_Name,$userSubject, $headers);
if(!$sentMail)
{
header('HTTP/1.1 500 Something went wrong.. Sorry!');
exit();
}else{
echo 'Dear '.$user_Name .', thank you for your email! ';
echo 'It has arrived to our inbox. We will get back to you within 2 business days.';
}
}
?>
答案 0 :(得分:0)
请说清楚,你需要做任何工作......然后使用jQuery ajax帖子。
$( "#subject" ).change(function() {
//url
//here set post to your php page
//response
});
答案 1 :(得分:0)
我面前没有确切的错误,但是PHP可能会在你的错误日志中吐出这样的错误:
Unknown constant userSubject, assuming $userSubject
$userSubject
当然是null
原因是$_POST
变量中的未引用键 - $_POST[userSubject]
。
将其更改为$_POST["userSubject"]
,它应该开始工作。
答案 2 :(得分:0)
最有可能只是错字,你的小提琴不起作用,因为第45行还有额外的括号(见控制台输出)。并且您的PHP脚本缺少引号:
$user_Subject = filter_var($_POST[userSubject], FILTER_SANITIZE_STRING);
更改为
$user_Subject = filter_var($_POST["userSubject"], FILTER_SANITIZE_STRING);