duanjuhuo8772 2013-05-11 17:17
浏览 26
已采纳

从javascript中检索后,只有1个字符而不是PHP数组中返回的整个单词

In Javascript I'm creating an array for a user side list

var dataArr = [];
 $("#sortable li").each(function(idx, elem) {
    dataArr[idx] = $(elem).html();
});
alert(dataArr[0]);

This is working as expected and will alert the first item in the list. "Frank" or whatever it may be.

$.ajax({
url: "fiddle.php",
type: "POST",
data: "dataArr="+dataArr,
success: function(response) {
alert(response);}

I send this array over to PHP and the ajax test confirms its retrieved from a var_dump on the other side.

echo ($_POST['dataArr'][1]);

The problem occurs here when trying to output a particular item, in this case the 2nd item which may be "John" it'll instead output the 2nd character in the first item "r". This is appearing in the Ajax test window. I'm looking for the whole word instead. Is it a syntax error or a problem with how the data is passed?

  • 写回答

2条回答 默认 最新

  • dongza3124 2013-05-11 17:29
    关注

    I think the problem is related to how you are sending your data in the ajax call.

    Try this:

    JS

    var dataArr = [];
     $("#sortable li").each(function(idx, elem) {
        dataArr[idx] = $(elem).html();
    });
    
    
    $.ajax({
        url: "fiddle.php",
        type: "POST",
        data: dataArr, //Send just the array
        success: function(response) {
            alert(response);
        }
    });
    

    PHP

    var_dump($_POST['dataArr']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?