Devan
Devan

Reputation: 170

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

Upvotes: -1

Views: 2093

Answers (2)

Rodolphe
Rodolphe

Reputation: 497

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

Upvotes: 0

Matt Mitchell
Matt Mitchell

Reputation: 41823

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).

Upvotes: 2

Related Questions