View Full Version : Changing a Variable without reloading the page

10-10-2007, 08:25 AM
Is there any way to change a PHP variable on a page without reloading that page.
Maybe with javascript?

Maybe something like:

<a href="#" onClick="change_variable()">Change variable</a>

10-10-2007, 01:03 PM
php can only be done on the server side, however you can use javascript (AJAX) to send data to the server and back without reloading the whole page, but just a portion.

read http://dynamicdrive.com/forums/showthread.php?t=19247

10-10-2007, 01:33 PM
I'm already using the 'Ajax Tabs Content script' found here: http://www.dynamicdrive.com/dynamicindex17/ajaxtabscontent/

Is there any way the I can modify this script to allow me to do what I want.

So that if someone clicks on one of the tabs it sends information to the server to change a variable on the current page.

10-10-2007, 10:53 PM
The PHP script used with the AJAX content script could be modified to store the requested page in the $_SESSION array, then just use that value from then on.

$_SESSION is sorta like cookies, but better, and it will be persistent through someone's visit to your site, rather than just existing in one instance of the script running, as with any standard variable in the code.

OR, you could actually just use cookies.

You can set (or read) a cookie with javascript, then read (or set*) that cookie with PHP, as $_COOKIE['cookiename'].
(*set would require use of a function, not just $_COOKIE = value;... set_cookie().)

10-11-2007, 07:57 AM
The $_SESSION option will be best as i have some experience of this already.
Plus it will allow me to destroy the session when certain other links are clicked on.

The only problem is that when i load the page initially, it requests content from the external page rather than having some 'intro text'
however i wouldn't want to set the session variable until one of the other tabs are clicked on. Is this possible?

If so would you know where i can get some script to do this? I have very little experience with javascript.

10-11-2007, 08:03 AM
<script language="text/javascript">
<?php echo "var a = 5;"; ?>

PHP is output as text to the browser. Javascript runs from that. You can use PHP to create parts of the JS as well.

Using the PHP, you can set the initial value, with ease.

You will want to set the value initially, to make life easier, just have a starting value of the first page you want displayed.

Have the script check if (isset($_SESSION['myvar'])) { dostuff() ; } and you can have an initial action, like setting that default value, etc.

10-11-2007, 08:12 AM
That is exactly what i'm trying to change:

if (isset($_SESSION['myvar']))
print "var enabletabpersistence=1";
print "var enabletabpersistence=0";

But what function actually sets the session variable? and how do I send that function from one of the clicked tabs?

10-11-2007, 08:27 AM
$_SESSION['myvar'] = 5;
echo $_SESSION['myvar']; //5

echoing text like above will be used in javascript, nothing directly related to sessions. Sessions are secure and entirely PHP, so you will output the values of session variables then you can use those values in javascript.

10-11-2007, 08:28 AM
I still don't fully understand how I set the session variable by clicking on a tab?

My thinking was:

<a href="external.php" rel="ajaxcontentarea" onclick="run_some_javascript()">TAB 1</a>

<script type="text/javascript" language="JavaScript">
function run_some_javascript()
some code here to set the session variable;

Do you know if this will work? If so, would you know of a script that will do this?

10-11-2007, 08:40 AM
No. You cannot access the session from javascript. It is entirely PHP.

Ajax is a javascript method of REQUESTING a PHP page... it runs PHP.
WHEN it is run, you will then, in that script being run, use a code to update the variable.

For example,
$_SESSION['choice'] = $_SERVER['REQUEST_URI'];

Remember, the art of Ajax is getting it to SEEM like php is executing client side, but it NEVER does. You must use javascript to get php to set the session, to update a link, etc.

You could instead use cookies as I described above. These are accessible from both PHP and Javascript without trouble. That's a nice option, though Sessions are a bit less intrusive.

10-11-2007, 10:55 AM
Very useful information, now works a treat, thanks! :)

12-25-2007, 03:02 PM
what is a code example of this, I have read loads of explanationson this forum. Seen no examples thou.

What would the code be to..

Grab variables from PHP, I'm guessing with an external PHP file (the php will update, no need to worry about the PHP code, I can do that).

Then update a div on a html page with AJAX ?