This is a discussion on Javascript and PHP ? within the PHP Language forums, part of the PHP Programming Forums category; Hi everybody !! I am just wondering if Javascript and PHP can interact with each other ? Let's say i have ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi everybody !!
I am just wondering if Javascript and PHP can interact with each other ? Let's say i have a variable in Javascript ( call it "var x = 6;") Is there anyway that we can use "x" value (which is 6) in PHP code? (let's say I want to print "x" out in PHP script) In general, can Javascript variables interact with PHP code and vice versa ? Thank you in advance !! PS: My initial thought is this: "It is impossible to do !" Prove me wrong ==> :) |
|
|||
|
Jay wrote:
> I am just wondering if Javascript and PHP can interact with each other > > Let's say i have a variable in Javascript ( call it "var x = 6;") > Is there anyway that we can use "x" value (which is 6) in PHP code? > (let's say I want to print "x" out in PHP script) > > In general, can Javascript variables interact with PHP code and vice > versa ? Javascript is a client-side technology. PHP is a server-sided technology. You can write out Javascript using PHP and you can post stuff in web forms and using GET URLs back to the server which can then be processed by PHP. In your example, you would need to assign the "x" variable to a hidden form value so it's actually posted back to the server, which PHP would then be able to use. There is no way for PHP to access Javascript variables, other than them being submitted in a form, modifying the form's post property or redirecting to another page in Javascript and passing the value as part of the URL. -- Chris Hope The Electric Toolbox - http://www.electrictoolbox.com/ |
|
|||
|
"Chris Hope" <chris@electrictoolbox.com> wrote in message
news:1083479178_317@news.athenanews.com... > Jay wrote: > > > I am just wondering if Javascript and PHP can interact with each other > > > > Let's say i have a variable in Javascript ( call it "var x = 6;") > > Is there anyway that we can use "x" value (which is 6) in PHP code? > > (let's say I want to print "x" out in PHP script) > > > > In general, can Javascript variables interact with PHP code and vice > > versa ? > > Javascript is a client-side technology. PHP is a server-sided technology. > You can write out Javascript using PHP and you can post stuff in web forms > and using GET URLs back to the server which can then be processed by PHP. > > In your example, you would need to assign the "x" variable to a hidden form > value so it's actually posted back to the server, which PHP would then be > able to use. > > There is no way for PHP to access Javascript variables, other than them > being submitted in a form, modifying the form's post property or > redirecting to another page in Javascript and passing the value as part of > the URL. > > -- > Chris Hope > The Electric Toolbox - http://www.electrictoolbox.com/ Then to follow up Jay's question, how do people get information about screen resolution, operating system, etc. on visitors to their websites? Everyone says it must be done through javascript, but javascript doesn't insert information into databases. And if PHP can't access javascript variables, then how can you get someone's screen resolution and put it into a database without visitors submitting a form? - JP |
|
|||
|
kingofkolt wrote:
> Then to follow up Jay's question, how do people get information about > screen resolution, operating system, etc. on visitors to their websites? > Everyone says it must be done through javascript, but javascript doesn't > insert information into databases. And if PHP can't access javascript > variables, then how can you get someone's screen resolution and put it > into a database without visitors submitting a form? That was one thing I forgot. You can use document.write() to write out stuff into the page as well. You could use this to write out a 1x1 pixel "image" which is actually a PHP script that passes in the screen width and height, image depth etc. Note that it still needs to be part of the url eg image.php?width=1024&height=768 (obviously you would construct this url using Javascript and the appropriate variables that contain the width and height and all the cross browser code you need to make it work correctly). For operating system and browser information it's passed as part of the user-agent string from the client to the server and is stored in the $_SERVER and $_ENV super globals; no need to use Javascript for that. Chris |
|
|||
|
Chris Hope wrote:
or > redirecting to another page in Javascript and passing the value as part of > the URL. Interesting. I hadn't thought of that, yet. Is creating a tiny/invisible new window from Javascript, and submitting form pages through it, a valid technique for setting up a vague kind of handshaking and client-server transfer? I haven't thought through the limitations and capabilities, but it might come in handy sometime. -- -- Lynn Wallace http://www.xmission.com/~lawall "We should not march into Baghdad. ... Assigning young soldiers to a fruitless hunt for a securely entrenched dictator and condemning them to fight in what would be an unwinnable urban guerilla war, it could only plunge that part of the world into ever greater instability." George Bush Sr. in his 1998 book "A World Transformed" |
|
|||
|
"Chris Hope" <chris@electrictoolbox.com> wrote in message
news:1083543879_1967@news.athenanews.com... > kingofkolt wrote: > > > Then to follow up Jay's question, how do people get information about > > screen resolution, operating system, etc. on visitors to their websites? > > Everyone says it must be done through javascript, but javascript doesn't > > insert information into databases. And if PHP can't access javascript > > variables, then how can you get someone's screen resolution and put it > > into a database without visitors submitting a form? > > That was one thing I forgot. You can use document.write() to write out stuff > into the page as well. You could use this to write out a 1x1 pixel "image" > which is actually a PHP script that passes in the screen width and height, > image depth etc. Note that it still needs to be part of the url eg > image.php?width=1024&height=768 (obviously you would construct this url > using Javascript and the appropriate variables that contain the width and > height and all the cross browser code you need to make it work correctly). > > For operating system and browser information it's passed as part of the > user-agent string from the client to the server and is stored in the > $_SERVER and $_ENV super globals; no need to use Javascript for that. > > Chris > Ok thanks. I had always heard and seen people collecting screen res. thru javascript but I never knew how. - JP |
|
|||
|
In article <5668e569.0405012200.18b17e8f@posting.google.com >, artlover70@yahoo.com (Jay) wrote:
>Hi everybody !! > >I am just wondering if Javascript and PHP can interact with each other? I had the same problem with a feature I wanted to do with passing a variable to a PHP script on a javascript popup and the PHP code to continue executing the code in the popup depending on the variables passed to it. I could not work out a solution, but after hours of searching (sorry didn't bookmark the URL), I did find a solution. Here's the code I have. Suppose that you want to call a popup, like I will. The script you want to call will call a Javascript popup and pass the variables from the initial click to the Javascript, which in turn will go to PHP to execute. So suppose you have a page which will call a popup like this: <script src="js/pics.js" type="text/javascript"> <!-- //--> </script> <body> <a href="#" onclick="go_open('doit.php?&gallery=01&picID=01'); "> </body> This URL will go and run the javascript "pics.js" script which contains something like: <!-- function go_open(url) { window. open(url,"Picture","width=50,height=50,toolbar=no, scrollbars=no, status=yes"); } //--> Now, at the VERY beginning of the "doit.php" page - you have to write the PHP / Javascript page like the following (before the <html> section). <?PHP // Import the variables from Javascript window.open() method to PHP. $G-ID = $_GET['gallery']; $P-ID = $_GET['picID']; ?> //--> </script> <head><title>Popup</title></head> <body> Normal HTML page here mixed with PHP to make use of the vaiables. Now later on in the page, you can then call the variables for $G-ID and $P-ID as you have imported them using Javascript and PHP. Complicated, but it does work. Dariusz |