0

I know this question is a duplicate but I really would like some help with this. I know the only way to send a php array to js is by encoding with json. Well I tried that but nothing works. Here's my code:

<?php

Header("content-type: application/x-javascript");
error_reporting(E_ERROR | E_WARNING | E_PARSE);

    $months = Array();

    $months = ['January'=>array(), 'February'=>array(), 'March'=>array(), 'April'=>array(), 'May'=>array(), 'June'=>array(), 'July'=>array(), 'August'=>array(), 'September'=>array(), 
                        'October'=>array(), 'November'=>array(), 'December'=>array() ]; 



    // Connect to MySQL
         if ( !( $database = mysql_connect( "localhost",
            "root", "" ) ) )                      
            die( "Could not connect to database </body></html>" );

    // open Events database
         if ( !mysql_select_db( "Events", $database ) )
            die( "Could not open Events database </body></html>" );

            foreach($months as $month => $arr) {

            $result = mysql_query("SELECT * FROM posted_events WHERE Month_ = '$month' ") 
                    or die ('Error updating database because: '.mysql_error());

            while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
                $months[$month][] =  $row['DayNum']; 

    }
}

            echo "var months = jQuery.parseJSON('$months');";
<?php

So the JS array variable 'months' should contain all the elements that was in the PHP's array variable right? Well it doesn't work. I don't know what's wrong. Help please? I need to use this array in my external JS file to do some more work with my program, so it's really important. Thanks guys.

2
  • 1
    you need to json_encode $months Commented Aug 13, 2013 at 15:08
  • 2
    Why not just do echo "var months = " . json_encode($months) . ";";? Commented Aug 13, 2013 at 15:08

1 Answer 1

1

You'll need to encode the JSON data using PHP. This can be done using json_encode(). Once it's encoded, you can pass the variable to Javascript, like so:

$months = json_encode($months);
echo "var months = $months ;";

Also, you're currently using jQuery.parseJSON. But, the jQuery documentation says:

The JSON standard does not permit "control characters" such as a tab or newline. An example like $.parseJSON('{"testing":"1\t2\n3"}') will throw an error in most implementations because the JavaScript parser converts the string's tab and newline escapes into literal tab and newline; doubling the backslashes like 1\\t2\\n3 yields expected results. This problem is often seen when injecting JSON into a JavaScript file from a server-side language such as PHP.

Use JSON.parse() instead.

json = JSON.parse(months);
for(var i in json) {

//code

}

Most browsers support this, but for those that doesn't, you can use json2.js.

Hope this helps!

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

4 Comments

Thanks that worked! Anything I need to do in my JS file to get to use the elements in the array? Cause I can't seem to get them
if it's possible, can you can show me the code on how to parse it?
@Kay: This has been asked and answered many times before. Please take a look at some of them (eg: stackoverflow.com/questions/1637334)
Ok getting some problem here with the JS part. Say for example, I want to alert elements from the array, its not working. Shouldn't this syntax be correct? alert(months[August][1]); Just as an example

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.