Solved

Store a variable after reload page

Posted on 2004-04-19
13
1,286 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Generate PDF from MySQL using PHP 3 55
Length of for loop to be dynamic 2 33
xml with php question 5 37
HTTPS for signup & login - HTTP for all other pages 10 51
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 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