简体   繁体   English

ajax serializearray不输出任何值

[英]ajax serializearray not outputting any value

I have a form HTML as follow: 我有一个表单HTML,如下所示:

<div id="error_message"></div>

<form action="daterequest.php"  id="daterequest_form" method="post">

            <select name="option1">
                <option value="1">1</option>
                <option value="0">0</option>
                <option value="2">2</option>
            </select>

            <input type="text" name="option2" >

    </form>

and I have JS script like this 我有这样的JS脚本

$(document).ready(function(){
$('#button_submit_form').on('click', function () {
  var data = $('#daterequest_form').serializeArray();   
  alert(data);

    $.ajax({
        url: 'daterequest.php',
        type: 'POST',
        data: data, 
        success: function(response) 
            {

                if (response == 'empty') { 
                  $('#error_message').text('ERROR MESSAFGE') } 
                else    {
                    $('#error_message').html(response);
                    $('#daterequest_form').fadeOut('400');
                }; 
            }
    });     
    e.preventDefault();
});


});

my alert(data); 我的alert(data); only gives me [object Object], [object Object] . 只给了我[object Object], [object Object]

I can't get the data to show in my alert.. I should see [option1 Value], [option2 inputvalue] 我无法在警报中显示数据。.我应该看到[option1 Value], [option2 inputvalue]

Also, once I figure out how to get the data in the alert, how to I retrieve it in PHP? 另外,一旦弄清楚如何在警报中获取数据,如何在PHP中检索数据? $_POST['what goes here']; ?

alert will not give you the details of the object use console.log() instead : alert不会使用console.log()来提供对象的详细信息:

console.log(data);

Take a look to Why is console.log() considered better than alert()? 看一看为什么console.log()被认为比alert()更好? .

Hope this helps. 希望这可以帮助。

There is no issue here - the problem is because you're using alert() to debug. 这里没有问题-问题是因为您使用的是alert()进行调试。 This means that the variable being displayed is coerced to a string, hence the array of objects is turned in to '[object Object], [object Object]' . 这意味着将显示的变量强制转换为字符串,因此将对象数组上载为'[object Object], [object Object]' Instead use console.log() to debug your code. 而是使用console.log()调试代码。

Also, from what you're trying to do I would suggest that using the serialize() method is more suited to your needs, and you should hook to the submit event of the form so that people using the keyboard also fire the event when the submit the form by pressing the Enter key. 另外,根据您的尝试,我建议使用serialize()方法更适合您的需求,并且您应该挂钩到formsubmit事件,以便使用键盘的人也可以在按下Enter键提交表单。 Try this: 尝试这个:

$('#daterequest_form').on('submit', function (e) {
    e.preventDefault();
    var data = $(this).serialize();   
    console.log(data);

    $.ajax({
        url: 'daterequest.php',
        type: 'POST',
        data: data, 
        success: function(response) {
            if (response == 'empty') { 
                $('#error_message').text('ERROR MESSAFGE') 
            } else {
                $('#error_message').html(response);
                $('#daterequest_form').fadeOut('400');
            }; 
        }
    });     
});

Then in your PHP you can retrieve the passed data by using $_POST and specifying the name of the form value: 然后,在PHP中,您可以使用$_POST并指定表单值的name来检索传递的数据:

var option1 = $_POST['option1'];
var option2 = $_POST['option2'];

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

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