简体   繁体   中英

javascript inside php code

i have place javascript inside php code, like this <?php if(condition) { ?><script>do some scripting here</script><?php } ?> it works perfect when i run it in local. but when i upload it to the server it doesnt run.. the javascript doesnt work. Can anyone please help. thanks, Devan

First, I'm pretty sure you have to do either:

<?php if (condition) { ?>  ** script stuff ** <?php } ?>

or

<?php if (condition) : ?> ** script stuff ** <?php endif; ?>

(Your script has no scope on the if statement, although that could be a copy/paste error).

Second, it could be your script (perhaps one of the Javascript files such as jQuery isn't on your server).

Your problem is pretty vague...

Here's all the things I would review first :

  • are you sure php runs on your distant server ?
  • do you have anything on screen when running your script ?
  • when you view the source code from the generated page : do you see your javascript code ?
  • did you check for javascript error messages ?

Another batch of questions after the comments :)

  • Did you try to change your JS code with a "basic" one, such as "alert('my code works');" ? (in order to see if your javascript code is triggered)
  • If the basic alert code does not produce a popup, then you should see your browser's setup with javascript. It may not be enabled
  • if it works (and you don't have any JS error messages) : try to place some more "alert" popups along your own code, in order to find where your code stops to work)

This will not provide you a direct answer to your problem, but will help you to find more details on your problem, and will help us find an answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM