Solved

Store a variable after reload page

Posted on 2004-04-19
13
1,281 Views
Last Modified: 2008-02-01
Recently I've asked a question about reloading page and I've got an answer which have the following code

document.location.href="<?=$PHP_SELF?>?id2="+obj.value;

It let me to keep a variable "id2" after reloading the page.
actually I am not quite understand the meaning of this piece of code, now I would like to store one more variable after reloading the page. It is just a variable declared in my php page, can I do this?
thanks
0
Comment
Question by:boy123
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility
Here

$PHP_SELF is the name of the script file. If you add variable value pairs at the end of the page and the reloads, the values are sent to the page using get method.

to add one more variable use this.
document.location.href="<?=$PHP_SELF?>?id2="+obj.value+"&id3="+newvalue;
0
 
LVL 12

Expert Comment

by:venkateshwarr
Comment Utility
and this value is accessed in the php script
as

$newvalue = $_REQUEST['id3'];
or
$newvalue = $_GET['id3'];
0
 

Expert Comment

by:Minutes
Comment Utility
It seems my page cannot reload now, I think I did sth wrong.
what should I put in the newvalue?

for example, if the variable in the php page is $test, then the sentence should be the following?

document.location.href="<?=$PHP_SELF?>?id2="+obj.value+"&id3="+$test;
0
 
LVL 7

Assisted Solution

by:madwax
madwax earned 100 total points
Comment Utility
@minutes

you have to differentiate between javascript and php. If you want to load a variable $test in the php page that the javascript link is going to it has to look like as follows:

document.location.href="<?=$PHP_SELF?>?id2="+obj.value+"&test=someValue";

in the php-page you get:

$id2 = $_GET['id2'] (as venkateshwarr wrote)
$test = $_GET['test'] (will get the value == someValue)

//jan
0
 

Author Comment

by:boy123
Comment Utility
Actually "someValue" is selected by the user and I don't know it's actually value until the user select it.



<?php

    $var  = "1";

?>

How can I pass the value 1 to $test?
0
 
LVL 7

Expert Comment

by:madwax
Comment Utility
by:

document.location.href="<?=$PHP_SELF?>?id2="+obj.value+"&test=<?=$var?>";
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 5

Accepted Solution

by:
crazycomputers earned 100 total points
Comment Utility
Sessions are a more convenient way to deal with this:

<?php

session_start();    // call this at the beginning of your script

// this code saves the values (you'd use this at the end of your script)
$_SESSION['obj1'] = serialize($obj1);
$_SESSION['obj2'] = serialize($obj2);

// this code gets the values
$obj1 = unserialize($_SESSION['obj1']);
$obj2 = unserialize($_SESSION['obj2']);
// ... etc

?>

$_SESSION is an array where you can store arbitrary data that will persist between sessions.  See http://www.php.net/session for more information on sessions.

serialize() turns a PHP structure (such as an array or even an object) into a string.  unserialize() turns a string obtained with serialize() back into the PHP structure it was before it was serialized.
0
 

Author Comment

by:boy123
Comment Utility
Sorry, but I just try to implement in this code but it doesn't work.

<html>
<head>
      <title>Main page for CSM</title>
</head>
<script>
function reload(form)
{
  document.location.href="<?=$PHP_SELF?>?select="+obj.value+"&scriptvalue=<?=$formvalue?>";

}
</script>
<body>
<?php
      $formvalue = "testing";  
                $scriptvalue1 = $_GET['scriptvalue'];
      echo $scriptvalue1;
?>
      
      <select name='select' onChange='reload(this)'>
      <option>a</option>
      <option>b</option>
      </select>
      
</body>
</html>
0
 
LVL 5

Expert Comment

by:TheClickMaster
Comment Utility
You have to give a value to $formvalue above the  

document.location.href="<?=$PHP_SELF?>?select="+obj.value+"&scriptvalue=<?=$formvalue?>";

line.





<html>
<head>
     <title>Main page for CSM</title>
</head>
<script>

<?php $formvalue="testing";  ?>

function reload(form)
{
  document.location.href="<?=$PHP_SELF?>?select="+obj.value+"&scriptvalue=<?=$formvalue?>";

}
</script>
<body>
<?php
                $scriptvalue1 = $_GET['scriptvalue'];
     echo $scriptvalue1;
?>
     
     <select name='select' onChange='reload(this)'>
     <option>a</option>
     <option>b</option>
     </select>
     
</body>
</html>
0
 

Author Comment

by:boy123
Comment Utility
Is it necessary for me to hard code the value of $formvalue, as I said, I don't know its value, it is selected by user.
0
 

Author Comment

by:boy123
Comment Utility
Use session is a good idea !!
finally I use session and it work fine
0
 
LVL 5

Expert Comment

by:crazycomputers
Comment Utility
Glad it worked!  =)

Can you close the session now?
0
 
LVL 5

Expert Comment

by:crazycomputers
Comment Utility
Oh you did close it.  Silly me.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now