3

So I ran into a problem and I couldn't really find a good solution anywhere. And I'm sure more people run into this problem.

I tried to have an Ajax script call to a php-script which echoes a JavaScript function again but this function wont run or activate. It however does show up in the code if you do inspect element.

So the html and Ajax is as follows. Its dummy code since my own is a bit more complicated. so any syntax errors I made here are not the solution since this works for other parts of my code.

<html><headbodyetc>
    <div id='change'>
        //is supposed to alert or call another js function after 
        //verifying something with a database for instance.
        <button type='button' onclick='ajaxbelow();'>alert</button>
    </div>
    <script type='text/javascript'>
        function ajaxbelow(){
        //AJAX code as found on w3schools.com/php/php_ajax_php.asp
        //calls the change.php
    </script>
</etc></html>

The php code that gets called is very simple.

//This shows up in the html-code after clicking the button but doesnt run.
echo"<script type='text/javascript'>alert('doenst work?')</script>";

So I am looking for a solution which makes me able to run a JavaScript or jquery function after an Ajax call, or the main reason why this doesn't work. Since I couldn't find it.

Inb4 why call the alert via php? Because I need to verify something first with the db on the server-side in my actual code.

5
  • Don't put your javascript in echo. Edit: Sorry where is the Ajax request ? Commented Apr 1, 2014 at 9:16
  • 1
    1: w3fools.com 2: at the place of echoing javascript back, which will never work. Put your function in javascript, then in the php echo something which will be tested in javascript, and based on the test you run the function you want. Commented Apr 1, 2014 at 9:17
  • 2
    This may be able to help you a bit more -> stackoverflow.com/questions/3514096/… Commented Apr 1, 2014 at 9:18
  • @CodeBird can you explain that better, since I tried echoing an element with an onload that runs an existing function but that didnt work either. Commented Apr 1, 2014 at 9:28
  • paste your function code, I'll edit it for you and explain. Commented Apr 1, 2014 at 9:44

4 Answers 4

2

So after combining and testing some of the comments I figured out my own answer. You cant create new javascript within the php echo. You can however echo an onload that calls an existing function. Onload only works for the following tags:

"body", "frame", "frameset", "iframe", "img", "input type="image", "link", "script", "style".

However in this case after testing some of them like "script" and "img" it still didn't work with all tags, but it did with the "style" tag. I didnt test all other tags though.

So that changed my code to:

<html><headbodyetc>
<div id='change'>
    //is supposed to alert or call another js function after 
    //verifying something with a database for instance.
    <button type='button' onclick='ajaxbelow();'>alert</button>
</div>
<script type='text/javascript'>
    //function to be called
    function test(){
        alert("now it works");
    }
    function ajaxbelow(){
    //AJAX code as found on w3schools.com/php/php_ajax_php.asp
    //calls the change.php
</script>
</etc></html>

and the php-code will then become

echo"<style onload='test();'></style>";

and now it does run the function.


edit this doesn't seem to work for IE, looking for a solution right now.

^ EDIT: By default, IE Browsers "DENY" the ability of scripts to throw prompts. So to enable this functionality, you must go to [Tools/ Internet Options/ Security / Custom Level / "Allow websites to prompt for information using scripted windows"] and enable that... Once you refresh, you will see your alert in IE :)

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

1 Comment

and I need to wait 2 days before accepting my own answer :/
0

Just add slashes before single quote as given below

   <?php echo '<script type="text/javascript">alert(\'doenst work?\')</script>'; ?>

2 Comments

tried that already doesnt run the alert though. I even tried echo-ing an element that has an onload which also doesnt work. Edit onclick does seem to work though :/
that's only for single & double quote problem. Pls check it.
0

You could use eval() to evaluate the returned javascript. The script tags wont be required if this method is used.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

1 Comment

To be honest I don't see how that would solve the problem, but if I hadn't found the answer already I would've tried it. still a usefull function though.
-1

Try this:

http://www.webdeveloper.com/forum/showthread.php?184830-Ajax-echo-script-problem

Basically the reason why nothing happens is because you are just sticking content in the DOM. Javascript is an event driven language and since nothing is telling the javascript to run at this point, its just sitting there doing nothing. If that code were there when the browser loaded the page, then the browser parsing the code is what would tell it to run. So, what you need to do is evaluate any scripts that come back

1 Comment

You should rework the code from that solution to apply to this example, or at least include an outline of it.

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.