Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
3  
Use JSON –  Marc B May 3 '12 at 18:38
1  
json_encode and json_decode are your friends –  Anigel May 3 '12 at 18:39
1  
And please use jQuery, this code looks like the stoneage... –  Rob Angelier May 3 '12 at 18:41
1  
Not enough jQuery –  Xeoncross May 3 '12 at 18:41
add comment

2 Answers

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 recommend 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.com/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!

share|improve this answer
add comment

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

share|improve this answer
 
is that the only way?? –  leo0110 May 3 '12 at 18:41
3  
No, but it's the only sane way. –  Ignacio Vazquez-Abrams May 3 '12 at 18:41
 
you could use serialize() and unserialize(), but you'll probably run into errors somewhere. JSON was created to do what you want. –  romo May 3 '12 at 18:42
 
so its like this var json = { 'info' : data}; –  leo0110 May 3 '12 at 18:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.