user1020496
user1020496

Reputation: 81

PHP JSON and <script> tag

I am using PHP to send a json string to jQuery ajax. I am simply using $("#id").html(data.result) to display the html from the fetched page. Now on the fetched page I am looping through MySQL results. I need to run a piece of javascript on each result so I can start a countdown timer for each result.

My problem is that the calling page simply displays the actual javascript script as a string.

$row_array['result'] .= '<script>trigger script</script>'

Any suggestions?

Thanks

Upvotes: 0

Views: 210

Answers (1)

Petr
Petr

Reputation: 3249

If part of data.result is javascript, it is not executed. Try this:

$script = 'alert("this is javascript");';
$html = '<b>blah blah this is html<b>';
$row_array['result'] = json_encode(array('script' => $script, 'html' => $html));

and then instead $("#id").html(data.result) use:

eval(data.result.script)
$("#id").html(data.result.html)

Upvotes: 1

Related Questions