2

///////UPDATE - I already have jquery library included to my code so if its easier with jquery than javascript let me know please.

OK. There are loads of questions on here that are sending a JavaScript array to php but only 1 which is the same as mine. Unfortunately I didn't understand the answer.

So, at the moment I have an associative array in php. I then used this code,

echo json_encode($this->_inputErrors);

I don't actualy know why i'm using it, just was mentioned a lot in other examples like this. So that then sends the data to javascript (via ajax) and if i do this code,

alert(requestText);

I get a long line of text. As I imagine i should.

So how do i then in javascript get the text back to an array?

Or Is there a better way to do this?

Many Thanks For Your Time,

Chris

1
  • json_encode converts a PHP object into a JSON one. Commented May 2, 2011 at 21:29

5 Answers 5

5
var o = JSON.parse( requestText );

Include this ( https://github.com/douglascrockford/JSON-js/blob/master/json2.js ) to support old browsers.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks For The Reply - I put the code that you wrote and then used alert to see its contents and it gave [object Object]
That's normal... It's a object... Try console.log( o ); and open your console, you'll see.
ok, 2 more questions - is the object an array object? And the link you gave me, Do not all browsers support json?
No it's not. It's an object. But objects in JavaScript work like associative arrays. And no, not all browser can parse JSON, that's why this function was written :)
But if you've got jQuery included, just use the code here: stackoverflow.com/questions/5862528/… jQuery already has a parser in it, there is no need to have 2.
3

requestText is a JSON string. You need to parse the string into an object.

You can use JSON.parse to convert the string to JSON.

var obj = JSON.parse(requestText);

If your browser doesn't have JSON, include this:

https://github.com/douglascrockford/JSON-js/blob/master/json2.js

1 Comment

If I include that file will it work for everyone's browsers who view my site?
0

You need to set the return type as JSON

or if using jQuery, you can use jQuery's method getJSON() to get the JSON object from the url

5 Comments

OP didn't say they were using jQuery.
Use this and then just do $.each looping trough all key => values
Ok, so what do you mean by "set the return type"? I assumed it was to do with $.ajax at first but now I don't get it. Do you mean like content-type in PHP or something else?
No use to include jQuery just for that -.-
Thanks For The Reply - How do I Do That? This is the code i'm using to get the data from php's echo /// requestText = xmlhttp.responseText;
0

Somedays before, I faced the same problem. Check my solution :)

array.html

$(document).ready(function(e){

    //This array is where I'll receive the PHParray
    var js_array=new Array();

    $("#btn").click(function(e){
             $.ajax({
                type:    'POST',
                 url:     'vector.php',
                 cache:   false,
                 success: function(data){

                    js_array=data;
                   //I use this "for" to print each array item into a div called "data".
                    for(var i=0;i<js_array.length;i++){
                         $("#data").append("<p>"+js_array[i]+"</p>");
                    }

                 },
                 dataType: 'json'
             });                  
    });                    

});

vector.php

<?php

$array = array(1,2,3,4,5,6);

/*As you, I use "json_encode" to serialize the array
echo json_encode($array);

?>

I hope it can help (:

Comments

-1

The simplest way to transform that long line of text in Javascript is using eval:

alert(eval('(' + requestText + ')'));

15 Comments

eval is evil. Even if you use it the correct way with (). Nothing tells you it's not a script. If you want to do it that way you would rather use JSONP. And btw, always prefer the function constructor to eval because it gives a scope. (yes, I did vote down)
@xavierm02: Stupid meme is stupid. There's nothing dangerous about evaling your own servers JSON response. And eschewing a quintessential feature of scripting languages for fictional reasons like that is not very clever. (It's also not fashionable to downvote competing answers.)
If you edit and replace eval by the Function constructor, I take back my downvote :-°
If you want to use eval, do it correctly as he does: github.com/douglascrockford/JSON-js/blob/master/json2.js
But you can avoid it (look at the other files in the repo).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.