Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 22-09-2009
Member
 
Join Date: Apr 2008
Posts: 264
How to embed Javascript inside PHP script

I'm fairly new to PHP and i was writing a script in which i wanted to have some javascript included. Please let me know How would one call some JavaScript inside the <? and ?> tags used in PHP? Is this possible?
Reply With Quote
  #2  
Old 22-09-2009
Member
 
Join Date: Feb 2008
Posts: 137
Re: How to embed Javascript inside PHP script

One of the lesser known sides of external JavaScript is the ability to reference a PHP file instead of the familiar .js file. I wouldn't use a NULL attribute at all, I'd use an isset() or !empty() function instead. I'd also adjust your <?php echo $var?> to simply adding a semi colon at the end of the variable.At first such an notion may seem strange, even impossible; after all, who among us isn't familiar with that barrier dividing server side and client side scripts that prohibit the two from interacting.

if (!isset...)
{ // not registered, use javascript to redirect
echo '<script type="text/javascript">'."\n";
echo '<!--'."\n";
echo 'location.href="http://localhost/restricted_resource.php"'."\n";
echo '// -->'."\n";
echo '</script>'."\n";
}
Reply With Quote
  #3  
Old 22-09-2009
Member
 
Join Date: Jan 2006
Posts: 6,878
Re: How to embed Javascript inside PHP script

PHP script can then get to these parameters using the global variable $HTTP_GET_VARS[]. So you're probably wondering at this point: "So what's the catch?" it just wont work with the browsers im using. maybe that's the problem - try removing it from your code and see what happens. if I were to use an external JavaScript within the <? ?> tags of PHP, would I have to reffer to a JavaScript in the following format:

echo "<SCRIPT SRC="javascript.js"></SCRIPT>";

Well, there is no catch really, just a few limitations. Since we are invoking the PHP script indirectly and via JavaScript, the final output of the PHP script needs to be valid JavaScript.
Reply With Quote
  #4  
Old 22-09-2009
Member
 
Join Date: Feb 2008
Posts: 130
Re: How to embed Javascript inside PHP script

The ability to reference a PHP script inside your external JavaScript can be very useful! As for your javascript, I cannot help you validate that, I'm not much of a javascripter. What I can say, is depending on what you are doing there may be a different way to do the same thing using arrays in javascript. It means your JavaScript now has access to once exclusive information on the server side, whether it's the server time, the visitor's IP address, a list of all the files within a certain directory, or mySQL database information.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to embed Javascript inside PHP script"
Thread Thread Starter Forum Replies Last Post
How to Pass a Javascript variable to a PHP script leshaspar Software Development 4 24-11-2010 10:48 AM
How to embed quotes in t-sql joel84 Software Development 3 03-08-2009 03:01 PM
Is there any embed flv player Mayur_Jain Software Development 3 30-06-2009 08:17 PM
Word 2008 + bibfuse: no script in script menu deval4u Software Development 5 06-04-2009 12:53 PM
Problem with script in order to open another script in an extra window Samsher Software Development 2 14-01-2009 01:25 PM


All times are GMT +5.5. The time now is 12:03 PM.