简体   繁体   English

通过Ajax发送下拉数据

[英]Send dropdown data through Ajax

I am using a contact form from a google search. 我正在使用Google搜索中的联系表格。 I cannot seem to figure out how to send a value from a dropdown list (select) through Ajax to a php file for a contact form. 我似乎无法弄清楚如何通过Ajax从下拉列表(选择)中将值发送到联系表单的php文件中。

Please check out my jsfiddle. 请查看我的jsfiddle。 http://jsfiddle.net/CBSeZ/ 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.';
    }
}
?>

make clear please, you need to do any work... then use jQuery ajax post. 请明确说明,您需要做任何工作...然后使用jQuery ajax发布。

$( "#subject" ).change(function() {
//url
//here set post to your php page
//response
});

I don't have the exact error in front of me, but PHP is probably spitting something like this out into your error log: 我眼前没有确切的错误,但是PHP可能会将类似这样的内容吐出到您的错误日志中:

Unknown constant userSubject, assuming $userSubject

$userSubject is, of course, null . $userSubject当然是null
The reason for that is your unquoted key in the $_POST variable - $_POST[userSubject] . 原因是您在$_POST变量中未加引号的键- $_POST[userSubject]

Change it to $_POST["userSubject"] and it should start working. 将其更改为$_POST["userSubject"] ,它应该开始工作。

Most probably just typo, your fiddle is not working because there are additional brackets at line 45 (see console output). 很有可能只是拼写错误,您的小提琴无法正常工作,因为第45行还有其他括号(请参阅控制台输出)。 And your PHP script is missing quotes: 而且您的PHP脚本缺少引号:

$user_Subject     = filter_var($_POST[userSubject], FILTER_SANITIZE_STRING);

change to 改成

$user_Subject     = filter_var($_POST["userSubject"], FILTER_SANITIZE_STRING);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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