如何使用ajax将数组从html传递到php

时间:2022-10-16 12:49:02

I try to pass array using jquery by ajax and back but my code not working well. I all the time change it but it is not working. please advise what to do. I think my jquery code is ok but php code not replay. thx.

我尝试用ajax和back传递jquery数组,但是我的代码不能正常工作。我一直在改变它,但它不起作用。请告诉我该怎么做。我觉得jquery代码没问题,但是php代码不会重放。谢谢。

html code.

html代码。

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1255">
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.5/jquery.min.js"></script>

<script type="text/javascript" >
$(document).ready(function(){
    var allVals = { 
              'a': '1', 
              'b': '2',
              'c': '3' 
            }; 
            //$.each(allVals, function(key, value) { 
              //alert(key + ': ' + value); 
                //  });
            });
$.ajax({
    type: "POST",
     dataType: 'html',
     url: "test4.php",
     data: 'allVals=' + allVals,
         cache: false,
         success: function(data)
         {
            alert(data);
            $('#test').html(data);
            }
         });
</script>

<title>Insert title here</title>
</head>
<body>
<div id="test">##</div>
</body>
</html>

php code test4.php

php代码test4 . php

<?php
    $allVals = $_POST['allVals'];
    if ($allVals != ""){
          foreach ($allVals as $key => $value) {
          echo ($key.' '.$value."<br />");
    }     }

?>

5 个解决方案

#1


3  

You can also send an array to PHP from jQuery like this. In your example your sending an object:

您还可以像这样从jQuery向PHP发送一个数组。在您的示例中,您发送一个对象:

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){
    header('Content-Type: text/html');
    print_r($_POST['allVals']);
    /*
    Array ( [0] => 1 [1] => 2 [2] => 3 ) 
    */
    die;
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function() {

    var myJavascriptArray = new Array('1', '2', '3');
    //or var myJavascriptArray=["1","2","3"]; 

    $.ajax({
        type: "POST",
        dataType: 'html',
        url: "test.php",
        data: {'allVals[]=' : myJavascriptArray},
        cache: false,
        success: function(data){
            $('#test').html(data);
        }
    });

});
</script>

</head>

<body>
<div id="test"></div>
</body>
</html>

#2


1  

You are doing the $.ajax-call outside the $(document).ready(...-scope. Move it inside!

你在做的是钱。- call(文档)时(美元以外的范围。内移动它!

I would do this part

我会做这部分

data: 'allVals=' + allVals

like so:

像这样:

data: allVals

And if you want to receive it as you do in php, change it to:

如果您想像在php中那样接收它,请将其更改为:

data: {"allVals": allVals}

Hope I got the syntax correctly.. ;)

希望我没弄错语法。,)

#3


0  

data: 'allVals=' + allVals,

this is the culprit, what gets sent is allvals[object Object]

这是罪魁祸首,发送的是allvals[object]

#4


0  

If you need a php array, I advise you to do like this :

如果您需要一个php数组,我建议您这样做:

data: {allVals : allVals}

数据:{ allVals:allVals }

The $_POST['allVals'] will contains an array.

$_POST['allVals']将包含一个数组。

Otherwise, If you only need single parameters you have to do as follows :

否则,如果你只需要一个参数,你必须做如下:

var stringToSend = sep = "";
$.each(allVals, function(key, value) { 
    stringToSend += sep+key+"="+value;
    sep = "&";          
});
$.ajax({
type: "POST",
 dataType: 'html',
 url: "test4.php",
 data: stringToSend,
 ...

#5


0  

You would be better using jQuery $.POST method:

最好使用jQuery $。POST方法:

var javascriptArray = new Array('a', 'b', 'c');

$.post('url_to_test4.php', {'allVals[]': javascriptArray }, function(data){
   // perform some action with the response data.
});

This will send to the PHP file an array named allVals, that will have the contents of your javascriptArray.

这将向PHP文件发送一个名为allVals的数组,该数组包含javascript tarray的内容。

#1


3  

You can also send an array to PHP from jQuery like this. In your example your sending an object:

您还可以像这样从jQuery向PHP发送一个数组。在您的示例中,您发送一个对象:

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){
    header('Content-Type: text/html');
    print_r($_POST['allVals']);
    /*
    Array ( [0] => 1 [1] => 2 [2] => 3 ) 
    */
    die;
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function() {

    var myJavascriptArray = new Array('1', '2', '3');
    //or var myJavascriptArray=["1","2","3"]; 

    $.ajax({
        type: "POST",
        dataType: 'html',
        url: "test.php",
        data: {'allVals[]=' : myJavascriptArray},
        cache: false,
        success: function(data){
            $('#test').html(data);
        }
    });

});
</script>

</head>

<body>
<div id="test"></div>
</body>
</html>

#2


1  

You are doing the $.ajax-call outside the $(document).ready(...-scope. Move it inside!

你在做的是钱。- call(文档)时(美元以外的范围。内移动它!

I would do this part

我会做这部分

data: 'allVals=' + allVals

like so:

像这样:

data: allVals

And if you want to receive it as you do in php, change it to:

如果您想像在php中那样接收它,请将其更改为:

data: {"allVals": allVals}

Hope I got the syntax correctly.. ;)

希望我没弄错语法。,)

#3


0  

data: 'allVals=' + allVals,

this is the culprit, what gets sent is allvals[object Object]

这是罪魁祸首,发送的是allvals[object]

#4


0  

If you need a php array, I advise you to do like this :

如果您需要一个php数组,我建议您这样做:

data: {allVals : allVals}

数据:{ allVals:allVals }

The $_POST['allVals'] will contains an array.

$_POST['allVals']将包含一个数组。

Otherwise, If you only need single parameters you have to do as follows :

否则,如果你只需要一个参数,你必须做如下:

var stringToSend = sep = "";
$.each(allVals, function(key, value) { 
    stringToSend += sep+key+"="+value;
    sep = "&";          
});
$.ajax({
type: "POST",
 dataType: 'html',
 url: "test4.php",
 data: stringToSend,
 ...

#5


0  

You would be better using jQuery $.POST method:

最好使用jQuery $。POST方法:

var javascriptArray = new Array('a', 'b', 'c');

$.post('url_to_test4.php', {'allVals[]': javascriptArray }, function(data){
   // perform some action with the response data.
});

This will send to the PHP file an array named allVals, that will have the contents of your javascriptArray.

这将向PHP文件发送一个名为allVals的数组,该数组包含javascript tarray的内容。