Jump to content

How to remeber variables in PHP


bobthebullet990

Recommended Posts

Hi guys, I've tried pretty much everything I can think of without using cookies, even tried sessions, but they don't seem to work either!

 

here's an example script I wrote to show my problem...

 

<?php
  $input = 1;
  $myval = "none";
  if (isset($_POST['test1'])) { $input=2; $myval="first"; $extra="extra";}
  if (isset($_POST['test2'])) { $input=3; $myval="second"; }
?>

<html>

  <form name="1" action="" method="post">
    <p>just press the button</p>
    <input type="text" name="in1" value="<?php echo $myval; ?>">
    <?php if ($input==1) { ?><input type="submit" name="test1" value="test1"><?php } ?>
<?php if ($input==2) { ?><input type="submit" name="test2" value="test2"><?php } ?>
<?php if ($input==3) { ?><p>finished, myval is <?php echo $extra; } ?></p>
  </form>
  
</html>

 

OK, So my problem is that I have a fairly large script with various forms, all I want to be able to is to get php to remember the values stored in variables and not reset all variables when the script is re-loaded, if you run the above script, the variable "$extra" is forgotten by the time I want to use it, how can I get php to retain this value without using a database or cookies?

 

Thanks...

Link to comment
Share on other sites

only problem doing that is there are 24 input boxes on the page! ...Its a script to configure test devices...

 

1. user enters config data for 8 devices

2. user chooses a filename and hits submit

3. script validates input, if no errors, then give file to user to download

4. script finds invalid input and asks the user to either download incomplete config file (maybe there are some broken devices that don't need a config) or to go back and change some values if they made a mistake.

 

The final bit is where I'm at, I've collected all the data and processed it, but now putting in step 3.5 where the script finds invalid data, then shows another button to continue anyway, as soon as that button is pressed I loose all data from the first button press!

 

So yeah, could do it your way, but there must be a simpler way surely?

Link to comment
Share on other sites

For the value of each element you wish to repopulate assign a php variable to it

 

<input type="text" name="something" value="$something_value">

 

Then in your script you would simply do

 

$something_value = $_POST['something'];

//Other code...

//Code to display the form(s)

 

 

 

Link to comment
Share on other sites

You essentially want a sticky form.  Something like:

<?php
   if(isset($_POST['submit']))
   {
      if(isset($_POST['in1']))
      {
         $in1 = $_POST['in1'];
      }

      /* validate and assign the other inputs */

      if(/* inputs all are valid */)
      {
         /* redirect the user to the file download page */
      }
      else
      {
         /* errors - generate error output, and store in a variable */
      }
   }
   else //form not submitted, or submitted with bad values
   {
      if(/* error output variable exists */)
      {
         /* output error */
      }
?>

<!-- HTML FORM -->

<form name="myForm" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
   <input type="text" name="in1" value="<?php if(isset($in1)) { echo $in1; } ?> />

   <!-- repeat with other inputs -->
</form>

<?php
   } //end else
?>

Link to comment
Share on other sites

Why are you not using sessions?

If using sessions, start each script with session_start(<-- click)

<?php
session_start(); /* * */
  $input = 1;
  $myval = "none";
  if (isset($_POST['test1'])) { $input=2; $myval="first"; $extra="extra";}
  if (isset($_POST['test2'])) { $input=3; $myval="second"; }
?>
<html>
  <form name="1" action="<? /* * */ echo $_SERVER['PHP_SELF']; ?>" method="post">
    <p>just press the button</p>
    <input type="text" name="in1" value="<?php echo $myval; ?>">
    <?php if ($input==1) { ?><input type="submit" name="test1" value="test1"><?php } ?>
   <?php if ($input==2) { ?><input type="submit" name="test2" value="test2"><?php } ?>
   <?php if ($input==3) { ?><p>finished, myval is <?php echo $extra; } ?></p>
  </form>
</html>

Any maybe some more changes here and there.

If you want to ensure that people who do not have cookies enabled should be able to access the page, then you can pass the PHP session id(<-- click) as a hidden value or as part of the link, but that is not wise from a security point of view - just good to understand the concept and get the thing working...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.