douxing6532 2013-07-18 14:54
浏览 16

javascript new Array - > php array

I have a page that has a bunch of tick boxes, these are identified with a name="a number". When a button is clicked, I have an JavaScript function that creates a new array with the numbers of the boxes ticked. This gets sent to a page 'test.php'.

It returns:

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

When I try and explode this array I get an array to string conversion error.

What am I doing wrong?

I need to get these numbers separated so I can do an SQL query for each.

JavaScript code:

<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 code:

<?
print_r($_POST);
?>

I know that I need to do something more on the test.php page but I would like to know what that is.

  • 写回答

1条回答 默认 最新

  • doujiacai4986 2013-07-18 15:01
    关注

    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.

    评论

报告相同问题?