最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to pass a javascript associative array in ajax to php file - Stack Overflow

programmeradmin2浏览0评论

How can i pass data (associative array) to php file.

 function ajax(url,data,callback)
        {
            var xmlhttp;
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                xmlhttp.onreadystatechange = function() {
                    if ( xmlhttp.readyState === 4 ) {
                        callback( xmlhttp.responseText );
                    }
                }
            }
            xmlhttp.open("POST",url,true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send(data);
        }

How can i pass data (associative array) to php file.

 function ajax(url,data,callback)
        {
            var xmlhttp;
            if (window.XMLHttpRequest)
            {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                xmlhttp.onreadystatechange = function() {
                    if ( xmlhttp.readyState === 4 ) {
                        callback( xmlhttp.responseText );
                    }
                }
            }
            xmlhttp.open("POST",url,true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send(data);
        }
Share Improve this question edited May 3, 2012 at 18:38 Marc B 361k43 gold badges431 silver badges505 bronze badges asked May 3, 2012 at 18:37 leo0110leo0110 971 gold badge4 silver badges9 bronze badges 5
  • 3 Use JSON – Marc B Commented May 3, 2012 at 18:38
  • 1 json_encode and json_decode are your friends – Anigel Commented May 3, 2012 at 18:39
  • 1 And please use jQuery, this code looks like the stoneage... – Rob Angelier Commented May 3, 2012 at 18:41
  • 2 Not enough jQuery – Xeoncross Commented May 3, 2012 at 18:41
  • youmightnotneedjquery. – lilHar Commented Feb 4, 2016 at 21:05
Add a ment  | 

2 Answers 2

Reset to default 8

The way i read this, you are in javascript, and you have a javascript object (javascript does not have associative arrays, just arrays and objects). So, whatever data you have, you need to turn it into a string and sent it via POST or GET to your php script. I would remend including JSON3 as a polyfill to ensure you will have JSON.stringify on your page (for cross-browserness). Code will be something like this:

var data = {
    someData: 'data',
    moreData: [
        'blah',
        'bleh'
    ]
};

var stringData = JSON.stringify( data );

// using jquery ajax for brevity (http://api.jquery./jQuery.ajax/)
$.ajax({
  type: "POST",
  url: "your-php-script.php",
  data: { data: stringData }
});

Now your php script can snag that string and turn it back into json itself:

<?php
    $dataString = $_POST['data'];
    $data = json_decode($dataString);

Hopefully this is what you were looking for. Cheers!

You can use PHP's JSON functions to achieve this. It will encode/decode arrays into a notation that javascript can handle.

发布评论

评论列表(0)

  1. 暂无评论