Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Keeping/not keeping variables when a form is submitted

Status
Not open for further replies.

ChocolateLover

Programmer
Jan 5, 2006
12
0
0
GB
I'm new to PHP.

I have a form that saves a record into a database.
If the required fields haven't been completed when the form is submitted, the form is redisplayed with a message and the fields that were already completed are retained using
Code:
session_start();
foreach($_POST as $key=>$value){
	$_SESSION[$key]=$value;
}
to save the inputs and
Code:
    <td><input type="text" name="author"
<?php
	if (isset($_SESSION['author'])) {
		echo ' value="' . $_SESSION['author'] . '"';
	}
?>	
></td>
to redisplay.
This works.

If the record has been saved successfully, I want to redisplay the form, ready to add a new record, but without the values from the previous record. What's the best way to do that?

I've tried setting
Code:
$_SESSION['record_saved']=TRUE;
when I know that the record has been saved but when the form is submitted or the page refreshed $_SESSION['record_saved'] is no longer set.

I'd really appreciate any help.
 
There is no reason to not use the "record_saved" variable.

What I generally do, though, is erase all session data that I do not need. Clear out all the session variables that store the form field values. Then on the next run of the script, there won't be any values available to prepopulate form fields.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks.

That's what I'm trying to do without much luck.
I'm trying to say
Code:
if isset($_SESSION['record_saved'])
then don't run
Code:
foreach($_POST as $key=>$value){
    $_SESSION[$key]=$value;
}
I'm definately setting $_SESSION['record_saved']. If I check it before submitting it = 1, after submitting it's not set.

What would be the reason for $_SESSION['record_saved'] no longer being set after the form submission - how could I be losing it do you think??

Thanks very much for your help.
 
Keep in mind how this will all play out as you go to insert the second record. Unless you remove it, $_SESSION['record_saved'] will always be there, an no record's data will be stored.

I recommend that you do something like:

if (!isset($_SESSION['record_saved'])
{
$_SESSION['record_saved'] = 0;
}

at the beginning of the appliation run. Then check for either a 0 or 1 in $_SESSION['record_saved'] as needed and set the value there as required.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top