2

TIP: "you could simple send an json object / array from php to js, and execute every entry like "update_match('1');" using the eval() function, please stop sending js code that way"- Lucian Depold.


In the index.php, I have this code, which executes when the document is ready:

$.post('php/main.php', {elements: 1}, function(return_msg) {
    alert(return_msg);
});

The respond I get is a bunch of scripts, as expected, with the expected values. However, they do not execute! How to make them execute?

Here is the response:

<script>jsfunction(37069);</script><script>updateTextbox('');</script><script>update_match('1', '19.30 Friday 15/5', '1');</script><script>update_player('1', '1', 'recoba', 'cmf', 'teo', '0', '0');</script><script>update_player('1', '2', 'nesta', 'cb', 'tsoulou', '0', '0');</script><script>update_player('1', '3', 'raul', 'cf', 'striker', '0', '0');</script><script>update_player('1', '4', 'samuel', 'cb', 'mafia', '', '1');</script><script>update_player('1', '5', 'deisler', 'cmf', 'free_kick', '1', '');</script><script>update_player('1', '6', 'ferdinard', 'cb', 'strong', '1', '');</script><script>update_match('2', 'Match 2', '0');</script>

When I had the PHP code that produced these scripts in the bottom of the index.php, all the js functions where called correctly. Because of this question though, I had to move the code to another .php file.

10
  • you want js code returned via ajax to run ? Commented Sep 13, 2015 at 21:33
  • Yes @Dagon, but I think I got the answer. Commented Sep 13, 2015 at 21:35
  • 1
    all good, just need clarification Commented Sep 13, 2015 at 21:37
  • are you sending all those "<script>" tags over ajax and execute them ??? Commented Sep 13, 2015 at 21:38
  • 1
    you could simply send an json object / array from php to js, and execute every entry like "update_match('1');" using the eval() function, please stop sending js code that way Commented Sep 13, 2015 at 21:42

3 Answers 3

2

Do it this way:

In PHP...

$arrayOfCalls = array();
$arrayOfCalls[]="update_match('1')";
$arrayOfCalls[]="update_match('2')";

$dummy = array();
$dummy['calls'] =$arrayOfCalls;
echo json_encode($dummy);

And in Javascript...

$.post('php/main.php', {elements: 1}, function(return_json) {
    return_json = JSON.parse(return_json);
    return_json.calls.forEach(function(code){
    eval(code);
    })
});
Sign up to request clarification or add additional context in comments.

9 Comments

Don't we need to specify in the post call that the data we sent are of JSON format?
the data you send to php, or from php to js ?
you could specify the fourth parameter as JSON, but jquery takes an "intelligent guess" (api.jquery.com/jquery.post). i think it should work without.
I had it working with: $.post('php/main.php', {elements: 1}, function(return_json) { //console.log(return_json); return_json = JSON.parse(return_json); return_json.calls.forEach(function(code){ eval(code); }) });
maybe the fourth parameter would also have done the trick, setting it to "JSON"...
|
1

You can append the result to your page :

$.post('php/main.php', {elements: 1}, function(return_msg) {
    $('body').append(return_msg);
});

The code will be executed but I'm not sure if it's safe to do that.

Comments

1

The date delivered by the AJAX call is just data - or text. In order to interpret that as JavaScript, you must append it to the dom.

Two ways: first, by appending the data directly:

$.post('php/main.php', {elements: 1}, function(return_msg) {
    alert(return_msg);
    $('body').append(return_msg);
});

but there's also a shorthand method:

$.getScript('php/main.php?elements=' + 1, function () {
   // script loaded

});

Please read the docs, there are some caveats!

1 Comment

Thanks! +1 for the shorthand method.

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.