0

I have a problem with executing a JavaScript function after an Ajax request with JQuery.

First, I call a PHP page with an Ajax request, everything works fine.

In this PHP page, I return a JavaScript call for a function.

But when I append this script on my HTML page, Firebug return this : "ReferenceError: getNumber is not defined" getNumber(55);. (55 is just an example, it can be from 0 to 100)

HTML (Jquery and myScript are loaded correctly):

<script src="jquery1111min.js"></script>
<script src="myScript.js"></script>
<div id="content_modal" class="content_modal">

</div>

JQuery (myScript.js):

$.get("inc/get_something.php", function(data) {
  $(".content_modal").html(data);//Works
});
function getNumber(nb) {
  alert(nb);//never called
}

PHP (get_something.php):

//some html
$number = mt_rand(0,100);
<script>getNumber(<?=$number?>);</script>

Cheers

Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35

2 Answers2

0

Is your

function getNumber(nb) { ... } 

oustide your

$(document).ready()

or

$(window).load()

If not, try putting it outside anything, in top of your tag

Thibaud Lacan
  • 346
  • 1
  • 6
  • 13
-1

You cannot execute javascript from a php page that isn't rendered in the browser. JavaScript is rendered in the browser and not on the server.

captain_jim1
  • 1,004
  • 1
  • 9
  • 18
  • he isn't trying to execute javascript from php, he's returning html that contains a script tag and some javascript code that is to be parsed and executed by the browser. – Kevin B Oct 15 '14 at 20:43
  • @KevinB You understand what I want to do perfectly :) – Magicprog.fr Oct 15 '14 at 20:50