ajax序列化数组不输出任何值


ajax serializearray not outputting any value

我有一个表单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>

我有类似的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();
});

});

我的CCD_ 1只给我CCD_ 2。

我无法获取要在我的警报中显示的数据。。我应该看看[option1 Value], [option2 inputvalue]

此外,一旦我弄清楚如何获取警报中的数据,我如何在PHP中检索它?$_POST['what goes here'];

这里没有问题-问题是因为您正在使用alert()进行调试。这意味着显示的变量被强制为字符串,因此对象数组被转换为'[object Object], [object Object]'。相反,使用console.log()来调试代码。

此外,根据您的尝试,我建议使用serialize()方法更适合您的需求,并且您应该挂接到alert(data);0的submit事件,这样当用户按Enter键提交表单时,使用键盘的人也会触发该事件。试试这个:

$('#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');
            }; 
        }
    });     
});

然后在PHP中,您可以使用$_POST并指定表单值的name来检索传递的数据:

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

alert不会提供对象的详细信息,而是使用console.log()

console.log(data);

看看为什么console.log()被认为比alert()更好?。

希望这能有所帮助。