Jump to content

[SOLVED] Server-side form value reloading


slurpophogus

Recommended Posts

OK, I've been searching for my answer here and not finding it... so it's time to ask for help.  This is probably a knucklehead question, but I'm a noob so be nice.

 

My site has a complex form that passes it's values on to a validation script using the POST method.  If validation fails, the validation script displays an error message to the user and presents a 'go back' button that takes him back to the form.  I'd like to reload the values into the form fields when the form page reloads, and I want to keep it all on the server side if possible.  Here's how my form looks (excerpt):

 

<?php

echo "

<td>
        Name: <input type='text' name='name' value='$_POST[name]'  />
       </td>
        ";

 

I realize that $_POST[name] will be null on the first pass, but that's fine because the text field comes up blank as expected.

 

Elsewhere on my form, there is a set of radio buttons.  I want the radio buttons to remember their last setting if the user returns.  That section looks a lot like this:

 

<td>
<input type='radio' name='type' value='tools' ";

if (!isset($_POST[type] or in_array("tools",$_POST)){
	echo "checked ";
	}

echo "/>Tools <br>

<input type='radio' name='type' value='supplies' ";

if (in_array("supplies",$_POST)){
	echo "checked ";
	}

echo " />Supplies
</td>";

 

My thinking here is that $_POST is loaded with the form values when the user first clicks the 'submit' button, and those values should still be available if he goes back to the form following a validation error.

 

But, alas... the fields come up blank.  Should I be using $_SESSION instead?  And if so, can I pass entire arrays (i.e., $var['str'] ) from $_POST to $_SESSION and then use the " if (in_array()" construct that I have in my code example?

 

TIA

Link to comment
https://forums.phpfreaks.com/topic/41396-solved-server-side-form-value-reloading/
Share on other sites

The easiest way is to use the same page for the form and validation, i.e. submit the form to itself. If the validation passes then do whatever you would do normally. Here's an example:

 

<?php

if (isset($_POST)) {
  //Validate the posted values
  if ($_POST['value1']=="IsValid") {
    //Process the form data then redirect to confirmation page
  } else {
  //Display error message
  }
}
?>
<form name="theform" action="<?=$PHP_SELF?>" method="POST">
Value 1: <input type="text" value="<?=$_POST['value1']?>">
<input type="submit">
</form>

Thank you! 

 

Actually, '$PHP_SELF' is way over my head.  Remember, I'm very new to PHP... and not an experienced coder either.  So, I'm still getting my head wrapped around the most basic tools.

 

But, I am pleased to report that, using a few hints that I found here on php freaks, I was able to solve my problem.  Here's how I did it...

 

At the top of the validation page I added:

foreach($_POST as $key => $value)
	{
  	$_SESSION[$key] = $value;
	}

$_SESSION[type] = array();
$_SESSION[type] = $_POST[type];

That last bit isn't very elegant, I know.  But I need 'type' to be an array, and I couldn't get the 'foreach' function to act recursively.  After adding the first part, all of the variable repopulated on the form just fine, except for the 'type' array, which corresponds to a set of 10 checkboxes.  If one clicked, say, 5 boxes, and then changed their mind and unchecked a couple... on the next visit to the form the 5 boxes would still be checked because the array was still set.  The $_SESSION[type] array would populate, but not de-populate if checkboxes were subtracted.  And if I 'unset' the array, I would get a parse error when the form was output because $_SESSION was now null, but also no longer an array which tripped up the 'in_array' test that I am using. So, I had to add the last two lines of code above, plus add the following to the top of the form page:

if (empty($_SESSION[type]))
	{
	$_SESSION[type] = array();
	}

Now it all works perfectly.  I realize that I probably have a couple extra lines of code that wouldn't have if I were more experienced... but my script works and I have lots of work left to do so I want to move ahead.

 

Thanks again, mjdamato.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.