2

I have a problem to get in a session array from php into a javascript

I´m setting the sessions array here, the print_r is only for checking so it´s correct

  while($row2[]=mysql_fetch_array($result2))
    $_SESSION['row2'] = $row2; 
  print_r($_SESSION['row2'][0][0]);
  print_r($_SESSION['row2'][0][1]);
  print_r($_SESSION['row2'][0][2]);

and in my javascript i use this, alert is only for checking

 var row2 =<?php echo json_encode($_SESSION['row2']) ?>;
 alert (row2[0][0]);
 alert (row2[0][1]);
 alert (row2[0][2]);

row2[0][0] and row2[0][1], works fine, I get the numbers from the array but row2[0][2] give me the value null, it should be some text in this field, it´s works fine with print_r($_SESSION['row2'][0][2]); in the php-code.

5
  • Can you view source in your browser and see what does var row2 = ... become? (actually, see the JSON on the javascript side) Commented Apr 10, 2012 at 13:56
  • {"0":"27","chapterNumber":"27","1":"0","number":"0","2":null,"nameOfTask":null} Commented Apr 10, 2012 at 14:00
  • Well, from this output it's clear as rain, index 2 is in fact null. If echoing $row2[ 0 ][ 2 ] prints a value, then the problem is either when inserting the value & then extracting it from the $_SESSION collection or with json_encode(), what is the value which should appear for $row[ 0 ][ 2 ]? Commented Apr 10, 2012 at 14:12
  • the value for $row[0][2] is "Rätt eller fel!" And I have discovered why it´s beeing null, it´s when I use swedish charactar å,ä,ö. When not using them it´s work!!! Commented Apr 10, 2012 at 14:31
  • Is it a new question how to solve this by using swedish charactar or should i update this question, I´m new here. And I need to use the swedish charactar!!! Commented Apr 10, 2012 at 14:36

3 Answers 3

1

use firebug console or chrome console. After that dump the entire row2 into the console and preview the data.

// JavaScript
var row2 =<?php echo json_encode($_SESSION['row2']) ?>;
console.debug(row2);

Also, keep in mind that all PHP associative arrays will become JSON object and should be accessed with .

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

1 Comment

Thanx it helped me a lot, I found the problem, it´s when I use swedish charactar å,ä,ö. When not using them it´s work!!!
0

You have to encode the Array, but keep in mind the result is a string, so you have to enclose it with single quotes and then parse it into an object:

var row2 = '<?php echo json_encode($_SESSION['row2']) ?>';
row2 = JSON.parse(row2);

1 Comment

gives the same result as before
0

As Alex stated, if you have an associative array into PHP, the json_encode output won't be the same.

Is it possible that it's the problem? As you seem not to use the associative keys, you may avoid to use mysql_fetch_array and could try mysql_fetch_row instead.

(Nb: your code is weird)

1 Comment

I have just finished a course in PHP and this is my first "work", I`ve never used JSON before I saw it on an other question here, I have to study that more. I don´t know if it´s the best way to get an PHP session array into javascript.

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.