Solved

Store a variable after reload page

Posted on 2004-04-19
13
1,285 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
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 7

Assisted Solution

by:madwax
madwax earned 100 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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

789 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