-4

Where am I going wrong with my programming logic here?

I have 2 php files. File 1 includes File 2. File 1 calls a php function from File 2. Inside the php function there is a bunch of html. The html works perfectly. At the end of the function I have this javascript....

<script type="text/javascript">
alert('hello');
</script>

This javascript isn't alerting "hello". What am I doing wrong?

Thank you in advance.

EDIT: New question because I skrewed the last one up.

In theory would the code below run properly? (yes/no)

<?php function AlertHelp(){ 
    ?><script>
        alert('help');
    </script><?
AlertHelp();
?>
3
  • 3
    can we see the other related code? how does the html souce llok like in the end? Commented May 26, 2012 at 20:36
  • 1
    yes, please. Use pastebin.com if necessary, and post the link back so we can help. Commented May 26, 2012 at 20:38
  • Please consider adding a code which reproduces your problem. What you have described in your question is too generic and probably nobody could answer your question. Also point out if you're using any libraries - php/javascript? Commented May 26, 2012 at 20:40

1 Answer 1

3

Long shot on a wild guess here with the limited information you gave.

My assumption is that you are not "including" the file via PHP's include, require, include_once or require_once functions, but are in fact using AJAX to load in the page's content.

If this is the case, then I shall also assume you're using innerHTML to put the content on the page.

Suddenly the solution is obvious: <script> tags added by innerHTML are not parsed and run. You could probably do something like this:

// assume `result` is the variable containing the AJAX response and `elem` the element it goes in
elem.innerHTML = result; // this doesn't change
result.match(/<script[^>]*>([\s\S]*?)<\/script>/i,function(m) {eval(m[1]);});

Please note however that eval should be avoided if possible. Consider redesigning your layout to use callbacks instead.

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

5 Comments

Hey Kolink, sorry for my rushed question. I have had that AJAX problem before but that's not quite the problem I'm here. My problem is I can't seem to get any javascript to run inside an actual PHP function. My question is, is that even possible? For example... <?php function AlertHelp(){ ?><script> alert('help') </script><? AlertHelp(); ?>
Didnt realize comments don't let your format. I hope you can read that.
Erm... no? PHP sends its result to the browser, and then the browser parses it. By all means, you can have: <?php function AlertHelp() {echo "<script>alert('help')</script>;} AlertHelp(); ?> but that only echoes the script to the browser, to be run when the page loads.
Interesting, So echoing out a statement is actually different than adding ?> and <? on either side of it?
Haha wow, I am impressed Kolink. I didn't realize, because my website is pretty much a labyrinth of code now, but I actually am funneling this file through AJAX. Thank you for the answer, you're awesome.

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.