473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Using PHP withing JS

Hi. Is it possible to use PHP code within a javascript funtion? For
example, is the following allowed...

foo( <?php $_SERVER['QUERY_STRING'] ?>);

function foo {
... does something with string
}

Thanks in advance.

Ziggi
Jul 16 '05 #1
4 7676
Ziggi wrote:
Hi. Is it possible to use PHP code within a javascript funtion? For
example, is the following allowed...

foo( <?php $_SERVER['QUERY_STRING'] ?>);

function foo {
... does something with string
}


Yes, here's an example:

test.php:
---------
<html>
<head>
<title>PHP/JS Test</title>
<script type="text/javascript">
function foo($x){
document.write('<font color="red"><b>'
+$x+
'</b></font> was passed to this function.');
return true;
}
</script>
</head>
<body>
<script type="text/javascript">
foo('<?php echo $_SERVER['REQUEST_URI'] ?>');
</script>
</body>
</html>

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 16 '05 #2
On Mon, 15 Sep 2003 22:46:50 +0100, "Ziggi" <on*******@hotmail.com> wrote:
Hi. Is it possible to use PHP code within a javascript funtion? For
example, is the following allowed...

foo( <?php $_SERVER['QUERY_STRING'] ?>);

function foo {
... does something with string
}

Thanks in advance.


Yes, but bear in mind that you're just getting PHP to output data that's
inserted literally inside the Javascript.

You can't go on and call a PHP function from a client-side Javascript event.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #3
Hmm, this code doesn't seem to work. I corrected the mistake in the line
foo('<?php echo $_SERVER['REQUEST_URI'] ?>'), ie escaping the single
quotation marks, but all i got was the line "was passed to this function."
almost as if the $_SERVER['REQUEST_URI'] has not been set... is this a
problem with my server?

Ziggi

"Justin Koivisto" <sp**@koivi.com> wrote in message
news:ly*****************@news7.onvoy.net...
Ziggi wrote:
Hi. Is it possible to use PHP code within a javascript funtion? For
example, is the following allowed...

foo( <?php $_SERVER['QUERY_STRING'] ?>);

function foo {
... does something with string
}


Yes, here's an example:

test.php:
---------
<html>
<head>
<title>PHP/JS Test</title>
<script type="text/javascript">
function foo($x){
document.write('<font color="red"><b>'
+$x+
'</b></font> was passed to this function.');
return true;
}
</script>
</head>
<body>
<script type="text/javascript">
foo('<?php echo $_SERVER['REQUEST_URI'] ?>');
</script>
</body>
</html>

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 16 '05 #4
Ziggi wrote:
"Justin Koivisto" <sp**@koivi.com> wrote in message
news:ly*****************@news7.onvoy.net...
Ziggi wrote:

Hi. Is it possible to use PHP code within a javascript funtion? For
example, is the following allowed...

foo( <?php $_SERVER['QUERY_STRING'] ?>);

function foo {
... does something with string
}
Yes, here's an example:

test.php:
---------
<html>
<head>
<title>PHP/JS Test</title>
<script type="text/javascript">
function foo($x){
document.write('<font color="red"><b>'
+$x+
'</b></font> was passed to this function.');
return true;
}
</script>
</head>
<body>
<script type="text/javascript">
foo('<?php echo $_SERVER['REQUEST_URI'] ?>');
</script>
</body>
</html>

Hmm, this code doesn't seem to work. I corrected the mistake in the line
foo('<?php echo $_SERVER['REQUEST_URI'] ?>'), ie escaping the single
quotation marks, but all i got was the line "was passed to this function." almost as if the $_SERVER['REQUEST_URI'] has not been set... is this a
problem with my server?


Well, there was no mistake in the code, I copied and pasted what I
posted and it worked fine. However, you will need to have PHP 4.1 or
greater to use the predefined $_SERVER array. Otherwise use
$HTTP_SERVER_VARS instead.

--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.

Jul 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Parslow | last post by:
Hi, I'm using an ASPTemplate class (the one from asptemplate.sourceforge.net) to produce WAP and HTML verions of the same pages. It seemed to me that it would be very usefull to have the...
13
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in...
3
by: Kevin Brown | last post by:
Is there anyway to generate this type of resulting HTML table from this XML using XSLT? Basically I need to be able to consult 2 trees of data to generate the HTML, but I have not been able to...
2
by: Frederik S | last post by:
Hello, I'm making a nice little login box in Javascript but having problems Posting the value inside a textfield. In a nutshell: I have a function: function getPostData (value)
3
by: amywolfie | last post by:
Hi All: I would like to run a report based on criteria from 3 unbound combo boxes located on a parameter form (combo boxes are: cboCuisine, cboLocation, and cboRestaurant) The present code...
1
by: Johan | last post by:
Hi all I'm trying to set the permissions of a file using WMI, and since i'm writing a windows service i want to use UNC paths doing this because the destination might not be a mapped path. I'we...
1
by: mike | last post by:
I have a WebApplication which is consuming a WebService. Basically the problem I am facing is such that everything is working fine when I am developing on my local machine. Then I do "Copy Project"...
2
by: Lans Redmond | last post by:
Is there anyway to connect to a socket via javascript. Im writing an application where IE on the client pc..needs to connect to a server and get some information before forward to an action class...
6
by: mast2as | last post by:
I have posted a few messages in the last few days about a project I am working on which is a quite simple parser. I ended up using the try/ catch structure as a general mechanism to control what's...
1
by: Ron T. | last post by:
I've made a table withing my Access file, that lists all the people that would be allowed to login using the database. The logins and passwords are logged within the table, and are referred to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.