Forum Moderators: coopster

Message Too Old, No Replies

Passing a value from Javascript variable to a PHP variable

JavaScript to PHP

         

aragornking

10:57 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



Hi. I wish to pass a javascript variable to a PHP variable within a JS function. Basically, I wish to submit a value to the function using a simple "onclick"/button approach, and have the function pass the value to the PHP variable. I realize this is a vasltly more difficult task (to me at least) than going from PHP to JS. Thanks.

bobnew32

10:59 pm on Nov 18, 2003 (gmt 0)

10+ Year Member



Is that possible? PHP is server side and javascript is client, so php would be queried first then javascript meaning you couldn't allow javascript to alter php. Php can alter javascript though. I'm new to this type of interaction though.

figment88

11:28 pm on Nov 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As bobnew32 points out PHP is server side and js is client side, so the js needs to make a call to the webserver. For this purpose I use modified webbugs (i.e. invisable images)

example web page:

<html>
<head>
<script>
webbug = new Image();
</script>
</head>
<body>
<form>
<input name=button1 type=button onclick="javascript:webbug.src=pix.php?variable=button1">
</form>
</body>
</html>

So now clicking the button (if I rembered all the quotes and parens right :) ) will cause the server to deliver up the file pix.php which can then excecute code:

For example,
<?
session_start();
$_SESSION['variable']=$_GET['variable'];
?>

I often use something like pix.php to insert things into a database I want to keep track of.

Have fun.

aragornking

5:51 am on Nov 19, 2003 (gmt 0)

10+ Year Member



Thanks figment88 (and bobnew32). It looks like "webbug" is the way to go. I tried to read up on webbug stuff, but could not get much of anything on the web.

I will try to get it to work over the next day or so.

figment88

3:37 pm on Nov 19, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not really much to read up on webbugs. They are just a deragatory name used for 1x1 tracking pixels also known as beacons. The people who refer to them as bugs are the self-appointed guardians of world-wide privacy.

Also, I don't know if anyone else uses them exactly the way I outlined. By changing the src with JavaScript you force the browser to download the file without displaying anything. This is better because some privacy programs and browser settings screen out traditional tracking pixels.

It's worse because it relies on the user's browser having JavaScript active, but you already are since you specificed an onClick handler.

Play around with the code I wrote. Remember as with other JavaScript tasks it is probably better to call a function with the onClick than do it inline if it gets any more complicated.

aragornking

5:52 am on Nov 21, 2003 (gmt 0)

10+ Year Member



Hi figment88. I have been playing with your code and I have to admit to feeling a little dense.

First of all, I presume that pix.php is a separate file/script that goes into the same directory as the main html/php file? I placed the following (your's basically) code in it:

<?php
session_start()?
S_SESSION['variable']=S_GET['variable'];
?>

Next, I created a file named webbug1.php with the first part of your code essentially verbatim:

<html> <head> <script>
webbug = new Image();
</script> </head> <body>
<form>
<input name="button1" type="button"
onSubmit = "javascript:webbug.src=pix.php?variable=button1">
</form> </body> </html>

I found I got an error if I used "onClick", but not with "onSubmit". I am a little confused about what is going on in the javascript code in the "onClick" part of the input line. Assuming I am anywhere near the right track with this approach, may I ask how I would proceed if I were to attempt to pass a value "magnet" to a PHP variable $action in the main script? Thanks for your patience.

figment88

7:03 pm on Nov 21, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey no problem.

My JavaScript was probably buggy. Calling JS from event handlers usually works a lot better if you call a function instead of doing it inline.

Anyway, I wrote and tested a little scrip that works. I also uploaded it a server on the net. If you want to see it, sticky me I'll give you the address.

Here's three files.

1) bug.html with two buttons
2) pix.php the bug that sets a php session variable
3) result.php lets you see the php variable

To test:
1) open both bug.html and result.php in different browser windows. Make sure they are going through a webserver with php installed instead of just through the filesystem.

2) click a button in bug.html

3) reload result.php

bug.html
==================
<html>
<head>
<script>
webbug = new Image();

function clk(param) {
iURL="pix.php?variable=" + param;
webbug.src=iURL;
}
</script>
</head>
<body>
<h1>Test Bug</h1>

<form>
<input name=button1 value=button1 type=button onclick="clk('button1');">
<input name=button2 value=button2 type=button onclick="clk('button2');">
</form>
</body>
</html>

pix.php
==========================
<?
session_start();
$_SESSION['variable']=$_GET['variable'];
?>

result.php
==========================
<?
session_start();
echo "<h1>Results</h1>";
echo "variable is " . $_SESSION['variable'];

?>

aragornking

11:02 pm on Nov 21, 2003 (gmt 0)

10+ Year Member



Thank you so much. I have PHP 4+, mysql, GD, etc. fully operational and a properly working Apache server. I created the files as you described and opened two browser windows, one for bug.html, the other for result.php. Bug.html gave me the expected title and two buttons. However, result.php gave me only:

Results
variable is

Despite various manners of refreshing both pages and clicking the buttons in the "bug" page and, I could not output a value for "variable" in the result.php page, presumably either "button1" or "button2" as the case may be. Looking at your code, it seems fine. I could not, by several means, extract a value from $_SESSION['variable'], so maybe there is a hitch somewhere. I will play with it further tonight.

I think I'll send a note via sticky to have a look at your uploaded files behavior.