Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1291
  • Last Modified:

Store a variable after reload page

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
boy123
Asked:
boy123
  • 4
  • 3
  • 2
  • +3
2 Solutions
 
venkateshwarrCommented:
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
 
venkateshwarrCommented:
and this value is accessed in the php script
as

$newvalue = $_REQUEST['id3'];
or
$newvalue = $_GET['id3'];
0
 
MinutesCommented:
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
Technology Partners: 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!

 
madwaxCommented:
@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
 
boy123Author Commented:
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
 
madwaxCommented:
by:

document.location.href="<?=$PHP_SELF?>?id2="+obj.value+"&test=<?=$var?>";
0
 
crazycomputersCommented:
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
 
boy123Author Commented:
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
 
TheClickMasterCommented:
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
 
boy123Author Commented:
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
 
boy123Author Commented:
Use session is a good idea !!
finally I use session and it work fine
0
 
crazycomputersCommented:
Glad it worked!  =)

Can you close the session now?
0
 
crazycomputersCommented:
Oh you did close it.  Silly me.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now