Jump to content

Pass a Javascript Variable to PHP


hoopplaya4

Recommended Posts

It is possible to make a link submit a Javascript variable to php at any time but for php to process it in the main page would require a page reload. So basically you setup a link to an invisible iframe and add to the iframe url variables. Then when the iframe loads, it gets the url variables and converts them to session variables. Then those session variables are available to any new page that loads. An example is as follows:

<? //index.php
echo "<a href='iframe.php?myvar=test&var2=example' target='iframe'>test link.</a>";
echo "<iframe width=1 height=1 scrolling='no' name='iframe' frameborder=0 src='iframe.php'></iframe>";
?>

<? //iframe.php
session_start();
foreach ($_GET AS $key => $val)
{
$_SESSION[$key]=$val;
}
unset($key);
unset($val);
?>

In the example above, as soon as the user clicks the link, the following variables will be set:

$_SESSION['myvar']=test

$_SESSION['var2']=example

So it is possible to make variables set at any time but it is just processing or displaying them which is the real trouble.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.