Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Pro PHP
|
Pro PHP Advanced PHP coding discussions. Beginning-level questions will be redirected to the Beginning PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 16th, 2005, 07:09 AM
Registered User
 
Join Date: Jan 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Mixing PHP and Javascript

Hi there,

Please could someone shed a little light on this problem!!

I have javascript creating a mouseover for a navigation part of my page. Then, I have a php program that connects to a mysql server so that the users can search for addresses. However, is it O.K. to mix javascript and php on the same page? I keep getting error messages.

In what order will the page execute? Will it execute the php first and then javascript?

Any help would be appreciated.

Thank you,

Neil



 
Old January 17th, 2005, 05:56 AM
Registered User
 
Join Date: Jan 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

PHP is executed on the server, javascript on the client. You can't 'mix' them. When you call a PHP page the server parses the entire thing and executes anything inside PHP tags. The resulting text is then passed to the user's browser where any client side code is executed.

 
Old January 17th, 2005, 01:24 PM
Authorized User
 
Join Date: Jan 2005
Posts: 82
Thanks: 0
Thanked 0 Times in 0 Posts
Default

PHP must be in PHP tags, i.e., <?PHP PHP CODE HERE ?>
if you put javascript within the php tags, make sure you are using the printing functions such as echo to output the javascript to the browser. Also, you must be careful to escape special characters like the quotes (").
but other than that, they can co-exist together as long as the flow of the page approves the mix.


 
Old January 25th, 2005, 12:33 PM
Authorized User
 
Join Date: Jan 2005
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to js_pandey Send a message via Yahoo to js_pandey
Default

PHP scripts run on server side and results in combination of HTML codes to client machine resulting in a complete page that user can see. Javscript runs on client machine.

two ways out:
1. if you are using a .js file(.js file is the file extension that contains all javascript function), then you can include this .js file using

<?
echo "<SCRIPT language=JavaScript src='script.js' type='text/javascript'>";
?>

anywhere in the php code, either in body part or in header part of php file.

2. if you are not including a .js file then:
you should write code like:

<?
echo "<SCRIPT language=JavaScript type='text/javascript'>\n";
//write javascript functions with echo command so that when php
//returns HTML code it look like simple javascript
echo "</SCRIPT>\n";
?>

i hope this clarifies.

regards,
-Jay
 
Old December 9th, 2006, 01:31 AM
Registered User
 
Join Date: Dec 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Actually, javascript can be run on the server as well. There is a runat attribute in the script tag that allows the script to run at the server, at least on IIS.

Mixing scripting languages can have odd effects, though. I was told
by our Microsoft instructor that an oddity in .asp pages is that the vbscript is all run first, then the javascript, regardless of which order the scripts occur on the page.





Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and Javascript davidhayter Beginning PHP 5 May 6th, 2005 05:33 AM
javascript with php kuehhc Beginning PHP 8 February 1st, 2005 10:59 PM
javascript and php? Ashleek007 Beginning PHP 21 September 23rd, 2004 01:40 PM
Mixing JavaScript and VBScript in ASP badgolfer ASP.NET 1.0 and 1.1 Basics 2 September 10th, 2004 09:34 AM
PHP with Javascript Droobles Pro PHP 1 February 17th, 2004 03:08 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.