0

The jquery script is printng " and , why?

php script:

<?php
session_start();

include 'classes/datahandle.class.php';

$data = new Datahandle();
$query = "SELECT i.instructionValue FROM INSTRUCTION_INPUT i WHERE i.deviceId = '$_SESSION[deviceId]' AND i.instructionState = '1' ORDER BY inputPortNumber";
$inputResult = $data->selectDataQuery($query);

while ($inRow = mysql_fetch_array($inputResult)){
    $array[] = $inRow[0];
}

header("Content-type: text/plain");
echo json_encode($array);
?>

jquery script:

<script>
$(document).ready(function() {

  var refreshId = setInterval(function() {  
    $.get('response.php', function(data) {
      $.each(data, function(index, value) {
        $('#value'+index).html(value).show();
      });
    });
  }, 3000);
  $.ajaxSetup({ cache: false});
});
</script>

HTML:

<tbody><tr style="color: white; font-weight: bold;">
    </tr>
        <tr>
        <td style="text-align: center; color: white; font-weight: bold;" id="value0">[</td>
    </tr>
        <tr>
        <td style="text-align: center; color: white; font-weight: bold;" id="value1">"</td>
    </tr>
        <tr>
        <td style="text-align: center; color: white; font-weight: bold;" id="value2">1</td>
    </tr>
        <tr>
        <td style="text-align: center; color: white; font-weight: bold;" id="value3">1</td>
    </tr>
        <tr>
        <td style="text-align: center; color: white; font-weight: bold;" id="value4">"</td>
    </tr>
        <tr>
        <td style="text-align: center; color: white; font-weight: bold;" id="value5">,</td>
    </tr>
        <tr>
        <td style="text-align: center; color: white; font-weight: bold;" id="value6">"</td>
    </tr>
    </tbody>
5
  • Why the jquery isn't printing the values from the php array correctly in each html id? At the moment is printing " and , and the int's are camming separated... Commented May 4, 2011 at 15:27
  • 1
    @FCC-PT: Can you show explicitly what you got, and what you were expecting? Commented May 4, 2011 at 15:30
  • Ok maybe html helps a bit... I'm requesting some data from my database and update table with id="value0" .... id="value100" etc Commented May 4, 2011 at 15:31
  • Please add you current json output to the question. Commented May 4, 2011 at 15:34
  • @FCC-PT: I believe this reproduces your issue? jsfiddle.net/XQ7cX Commented May 4, 2011 at 15:36

1 Answer 1

2

You need to use $.getJSON, which parses the JSON object retrieved from the AJAX request, instead of trying to process a raw string:

$.getJSON('response.php', function(data) {

Also, as suggested by @Rocket, you should change header("Content-type: text/plain"); to header("Content-type: application/json"); in response.php.

Here is the version without parsing JSON, which reproduces your problem: http://jsfiddle.net/XQ7cX/.

Here is the version with parsing JSON, which shows correctly: http://jsfiddle.net/XQ7cX/1/

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

1 Comment

You should also change header("Content-type: text/plain"); to header("Content-type: application/json");

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.