0

我有一个页面有一堆勾选框,这些都用名称“数字”标识。单击按钮时,我有一个 JavaScript 函数,该函数创建一个新数组,其中勾选了框的编号。这被发送到页面“test.php”。

它返回:

Array
(
    [data] => [null,"47284","47282","47281","47280","47279","47278","47277","47276","47269"]
)

当我尝试分解这个数组时,我得到一个数组到字符串的转换错误。

我究竟做错了什么?

我需要将这些数字分开,这样我就可以对每个数字进行 SQL 查询。

JavaScript 代码:

<script>
var selected = new Array();

function showMessage() {
var selected = Array();

    $('input:checked').each(function() {
        selected.push($(this).attr('name'));
    });
    var answer = confirm('Are you sure you want to send these emails again?');
    if(answer) {
    var jsonString = JSON.stringify(selected);
    $.ajax({
        url: "../../test.php",
        type: "POST",
        data: {data : jsonString}, 
        success: function(data){
            alert(data);
        }
    })
    }
}
</script>
<SCRIPT language="javascript">
$(function(){

    // add multiple select / deselect functionality
    $("#selectallsent").click(function () {
          $('.yes').prop('checked', this.checked);
    });
    $("#selectallprogress").click(function () {
          $('.no').prop('checked', this.checked);
    });

    // if all checkbox are selected, check the selectall checkbox
    // and viceversa
    $(".yes").click(function(){

        if($(".yes").length == $(".yes:checked").length) {
            $("#selectallsent").prop("checked", "checked");
        } else {
            $("#selectallsent").removeAttr("checked");
        }

    });
    $(".no").click(function(){

        if($(".no").length == $(".no:checked").length) {
            $("#selectallprogress").prop("checked", "checked");
        } else {
            $("#selectallprogress").removeAttr("checked");
        }

    });
});
</script>

test.php 代码:

<?
print_r($_POST);
?>

我知道我需要在 test.php 页面上做更多的事情,但我想知道那是什么。

4

1 回答 1

0

Two things you need to do ....

First one is to not stringify the selected object (jquery will deal with this for you)

function showMessage() {
var selected = Array();

    $('input:checked').each(function() {
        selected.push($(this).attr('name'));
    });
    var answer = confirm('Are you sure you want to send these emails again?');
    if(answer) {
    $.ajax({
        url: "../../test.php",
        type: "POST",
        data: {data : selected}, 
        success: function(data){
            alert(data);
        }
    })
    }
}

Then on the PHP side you can just access the values via

print_r($_POST["data"]);

This will be an array of your inputs.

于 2013-07-18T15:01:42.680 回答