Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Store a variable after reload page

Posted on 2004-04-19
13
Medium Priority
?
1,289 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 12

Expert Comment

by:venkateshwarr
ID: 10862431
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
ID: 10862443
and this value is accessed in the php script
as

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

Expert Comment

by:Minutes
ID: 10862616
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:madwax
madwax earned 400 total points
ID: 10863021
@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
ID: 10863269
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
ID: 10863297
by:

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

Accepted Solution

by:
crazycomputers earned 400 total points
ID: 10864529
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
ID: 10864933
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
ID: 10865565
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
ID: 10867054
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
ID: 10868734
Use session is a good idea !!
finally I use session and it work fine
0
 
LVL 5

Expert Comment

by:crazycomputers
ID: 10870711
Glad it worked!  =)

Can you close the session now?
0
 
LVL 5

Expert Comment

by:crazycomputers
ID: 10870723
Oh you did close it.  Silly me.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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