Hi I have the following problem. I'm trying to access the data i query from database in javascript and then execute a function

<?php

    $sql_sp1 = "{call [IR_CMS].[dbo].[usp_GetPropertyPriceAndAvail](?,?)}";
    $params = array( array($ChaletID, SQLSRV_PARAM_IN),
    array($BoardBasisID, SQLSRV_PARAM_IN));
    $result_sp1 = sqlsrv_query( $conn, $sql_sp1, $params);
    if( $result_sp1 === false)
      {
        echo "2. Get property price and avail - Error in query preparation/execution.\n";
        die( print_r( sqlsrv_errors(), true));
       }
    $data_sp1 = array(); 
    while($row_sp1 = sqlsrv_fetch_array($result_sp1, SQLSRV_FETCH_ASSOC)) {
    $data_sp1[] = $row_sp1;

   }
     //echo '<pre>';
     //print_r($data_sp1);
     //echo '</pre>';

?>
<script>

availdata = <?php echo json_encode($data_sp1); ?>;

</script>

If I echo the data it produces something like this

[{"PropertyID":138,"PropertyName":"Yves","ShowAvail":"Y","PropertyStartDateID":509495,"StartDate":"2012-12-08","DurationDesc":"7 nights","Saving":null,"Price":null,"Avail":"4","LiveSeason":1,"SeasonDesc":"2012-13","BoardBasisDesc":"Catered","PropTypeDesc":"Chalet Hotel","ResortName":"Les Arcs","CountryDesc":"France","FTA":"FTA","Sole_Use":0,"IsInShorlist":0}];

but I cannot access it in the javascript. It will work if I copy and paste the data into my javascript directly, but not if I try to access it dynamically. And yes, I have done my research, I'm pretty sure this should work Any ideas whatI'm doing wrong

share|improve this question
3  
How are you trying to access the data? – albin Jun 18 at 10:29
How are requesting this data, are you passing JSON headers from your PHP e.g. Content-Type "application/json". You should use jQuery's getJSON, or set the dataType to JSON in your Ajax request. – StuR Jun 18 at 10:29
1  
Can you show us the code which doesn't work? – blockhead Jun 18 at 10:38
feedback

2 Answers

try this:

JSON.parse(your_data);

share|improve this answer
feedback

Seems like your code should work. The easiest way to debug it and move forward with the problem is to view the HTML source.

simply load the page, right click and choose "view page source".

My guess is that encode_json does not reproduce what you think it does - probably creates invalid JSON or some JSON structure other than the one you expected.

If you copy/paste the HTML here it will be easier to help you.

I also recommend using "chrome" with the developers' pane open ( F12 or ctrl+shift+j ) - this will make every javascript error visible right away.

If you prefer firefox - the firebug is the way to go. Explorer also have a developers' pane available with F12.

Try to debug the code accessing availdata - simply write debugger; before you try to access it. make sure the developers pane is open, and make sure to remove the debugger; before release.

These steps will help you to resolve the problem quickly, or at least provide more info for us to help. Please let me know if there is any progress.

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
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.