Take the 2-minute tour ×

Hi does anyone know how to stop json data from outputting in the browser before the page loads in? The data being echoed in included php script with

echo json_encode($row);

But 'echo' is what gives me access to the data in my $.ajax functions.

Thanks in advance

share|improve this question
sounds like a js syntax error to me. but with out seeing code, its any ones guess –  Dagon May 11 '11 at 21:48
Can you post a relevant excerpt of the generated HTML? –  user212218 May 11 '11 at 22:11

3 Answers 3

up vote 1 down vote accepted

From what you are saying in your question and your comment on an earlier answer, you have a voting.php script which includes another script, say data.php. The latter does echo json_encode($row), which causes unwanted data in the output from voting.php. You want voting.php to load the data from data.php using the jQuery $.ajax function.

  1. Don't include data.php in voting.php.
  2. Make the necessary $.ajax calls after the page produced by voting.php has loaded, i.e. when the DOM is ready, using the jQuery ready event.

The ready event:

$(document).ready(function() {
  // $.ajax call which loads data from data.php
share|improve this answer
Yeah thanks @havard. It was happening because I was using the data twice, through the included script and also through the newly implemented $.ajax call which was bringing in JSON through the same included script. –  L-Samuels May 11 '11 at 22:12

Wrap your code like this, it will only fire once the document is loaded.

$(document).ready(function() {
    // put all your jQuery goodness in here.
share|improve this answer

PHP, by definition, can ONLY execute before the page loads.

If you give a little more info on what you're trying to accomplish, I'm sure you'll get some useful suggestions.

share|improve this answer
PHP (usually) executes as the page loads, not before. –  Håvard S May 11 '11 at 21:48
More like PHP executes and generates the page, which may or may not continue as the page is sent to the client. –  Marc B May 11 '11 at 21:52
Okay I have got a 'voting' page that brings in some JSON data through an $.ajax function. I get the data and use it fine. But its just that now before the voting page loads up the browser outputs the unwanted json data echo from the included php script. –  L-Samuels May 11 '11 at 21:53
Its weird because its seems to work fine on a desktop browser but the problem occurs testing on an iPhone. Thanks anyway guys. –  L-Samuels May 11 '11 at 21:59

Your Answer


