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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Forms

Status
Not open for further replies.

gokeeffe

Programmer
Jan 11, 2005
170
IE
Greetings from Ireland

Could anyone give me some advice on thie problem.
I have a three step registration form with a confirmation page where you click the submit button, where the values then go to a mysql database. My problem is that if someone wants to change a field and they click the back button then the information that they have entered has disappeared on the forms. Is there anyway that this information could be keep on the previous steps until the submit button is pressed.

Any help much appreciated
Gaz

 
Hi,

You can do this in several ways.
I think you might want to consider using sessions.

At the top of each page, you do:
Code:
session_start();
ps. you cannot have *anything* parse *anything* before you use the session_start();

After the session is started, you might want to fill some session variables.

Code:
if (!($_SESSION['name'])) {
    $_SESSION['name'] = addslashes(strip_tags(trim($name)));
  }

How do you use the variables?

Code:
<input type="text" name="name" value="<?=$_SESSION['name']?>" />

ps. you can also have the three steps in one file!

Code:
switch ($submit) {
case "step_2":
   /*Show step_2*/
   break;
case "step_3":
   /*Show step_3*/
   break;
default:
   /*Show step_1*/
}

When you make your form, make sure that you then do it like:
Code:
<form action="?" method="post">
  here you have form fields, etc.
  <input type="submit" name="submit" value="step_2" />
</form>

Olav Alexander Mjelde
Admin & Webmaster
 
Tks DaButcher for your quick reply

Yes I am using session variables in my form.

Some of my fields are input types and some are drop down's

Example of dropdown code

// Display the Junior Cert Level
$query_result_two = mysql_query ('SELECT * FROM county ORDER BY county_name');
while ($row = mysql_fetch_array ($query_result_two, MYSQL_NUM))
{
echo "<option value=\"$row[0]\">$row[1]</option>\n";
}

Example of input field code

<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<p>
<input type="text" name="first_name" size="30" maxlength="15"
value="<?php if (isset($HTTP_POST_VARS['first_name'])) echo $HTTP_POST_VARS['first_name']; ?>" />
</p>

Hope this gives you some indication of what I am doing.

Tks

Gaz

 
well, it's quite easy then..
check if the session variables excist:

Code:
<?php
if (!($_SESSION['name'])) {
/* This only runs if the session name is not set */

}
?>

it's almost like a login script.. If the session is not set, the user is not logged in.

Only set the session, if the input is correct.

If input is incorrect, flag it and refuse to move on.. (tell user what is wrong)

Olav Alexander Mjelde
Admin & Webmaster
 
Yes I have validated my inputs and set my sessions

It's just when I am on my confirmation page if I want to go

back and change data from the original inputs the values

are not present. It would make it alot easier for users

to see the information they have selected or inputed without

having to fill out fields again. Is there any code I can add

to this for example

<form action="<?php echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">

<input type="text" name="first_name" size="30" maxlength="15"

value="<?php if (isset($HTTP_POST_VARS['first_name'])) echo $HTTP_POST_VARS['first_name']; ?>" />

to keep the values present until the data is enetered into

the database.

TKs

Gaz
 
DaButcher can you change the code below to show me how to

keep this info displayed even if I click back to return to

this page.

/*********** Segment of input form **************/

<tr>
<td colspan="2" class="labelcell"><label for = "town_region_name">Town/Region</label></td>
<td colspan="2" class="fieldcell2"><input type="text" name="town_region_name" size="20" maxlength="20"
value="<?php if(isset($_POST['town_region_name'])) echo $_POST['town_region_name'];?>"/>
</td>
</tr>


/*********** Entire code of this page **************/


<?php # index.php

// This is the main page for the site

// Include the configuration file for error management and such
require_once ('includes/config.inc');

// Set the title and the html header
$page_title = 'Step Two';

// Include the header file
include_once('includes/header.html');

require_once ('../mysql_connect.php'); // Connect to the Database
?>

<div id = "contentform">

<?php

if (isset($_POST['submit']))
{ // Check if the form has been submitted



if (($_POST['county_id']) == 1)
{ // Validate the secondary exam

$u4 = FALSE;
echo '<p class="validation">You forgot to select a county !</p>';
}

else
{
$u4 = escape_data($_POST['county_id']);
}

// Check for a town/region
if (eregi ("^[[:alnum:]_]{4,30}$", stripslashes(trim($_POST['town_region_name']))))

{
$u5 = escape_data($_POST['town_region_name']);
}

else
{
$u5 = FALSE;
echo '<p class="validation">Please enter a valid town/region!</p>';
}




if ($u4 && $u5)

{ // If everything is ok


// Get the users input from the form

$county_id = $_POST['county_id'];


// Start the session register the values and redirect

$_SESSION['county_id'] = $row[0];
$_SESSION['town_region_name'] = $_POST['town_region_name'];
$_SESSION['extra_location_details'] = $_POST['extra_location_details'];

ob_end_clean(); // Delete the buffer
header("Location: . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "step3.php");
//header('Location:
exit();

}

} // End Of Check if the form has been submitted

?>



<form name = "signup" id="signup" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset>
<legend>Step Two &nbsp;<font size="-2"><font color="#666666">County - Town - Additional</font></font></legend>
<h2>
Select County : Provides you with the option to select your county of choice</br></br>
Select Town/Region : Allows you to specify where exactly you would like to provide grinds eg. Dublin 4,Clonmel,Mallow</br></br>
Select Additional Location Details : Optional if you would like to be more specific</br>
</h2>

<?php




echo'<table align="center">';

echo'<tr>
<td colspan="2" class="labelcell"><label for="county_id">Select County:</label></td>';

echo'<td colspan="2" class="fieldcell2">

<select name="county_id">';

$query_result = mysql_query ('SELECT * FROM counties ORDER BY county_id');

while ($row = mysql_fetch_array ($query_result, MYSQL_NUM))

{
echo "<option value=\"$row[0]\">$row[1]</option>\n";
}
echo'</select></td>';

echo'</tr>';


mysql_close();


?>


<tr>
<td colspan="2" class="labelcell"><label for = "town_region_name">Town/Region</label></td>
<td colspan="2" class="fieldcell2"><input type="text" name="town_region_name" size="20" maxlength="20"
value="<?php if(isset($_POST['town_region_name'])) echo $_POST['town_region_name'];?>"/>
</td>
</tr>



<tr>
<td colspan="2" class="labelcell"><label for = "extra_location_details">Additional Location Details</label></td>
<td colspan="2" class="fieldcell2"><input type="text" name="extra_location_details" size="100" maxlength="100"
value="<?php if(isset($_POST['extra_location_details'])) echo $_POST['extra_location_details'];?>"/>
</td>
</tr>

</td>
</tr>
</table>

</fieldset>
<input type="submit" name="submit" value="Step Three" class="btn"/>

</br>
</br>

<?php
// Include the footer file
include_once('includes/footer.html');
?>

TKs alot

Graham
 
Code:
<input type="text" name="town_region_name" size="20" maxlength="20" 
value="<?php if(isset($_SESSION['town_region_name'])){ echo $_SESSION['town_region_name'];?>"/>

I dont see you calling session_start(); ?
it should be at the very top of your page, just below <?php

Instead of that variablechecking there, I would do something like this, above the form:
Code:
if (!($_SESSION['town_region_name'])) {
  $_SESSION['town_region_name'] = "";
}

Then, since it's "", but not NULL, you can parse it without raising errors in the log.


Code:
<input type="text" name="town_region_name" size="20" maxlength="20" 
value="<?=$_SESSION['town_region_name']?>"/>


I think that is much easier to read.

Olav Alexander Mjelde
Admin & Webmaster
 
I've called session_start(); in my header file which I call

at the start of the code. If I use your method will it keep

the data that was entered on the page even if I navigate

back to change it.

Gaz
 
you have to do one thing though!!
you have to edit where you fill the session variables..

eg.
if (isset($_POST['foo'])) {
if (($_SESSION['foo'] != $foo) || !($_SESSION['foo']) {
$_SESSION['foo'] = $_POST['foo'];
unset($_POST['foo']);
}
} // end if isset($foo)

you can put this in the top of your page I think, if they are all in one php page that is.

it will then check if foo is set.. (if submitted).
if it is set, if it's not the same as the session foo, or if the session foo does not excist, set the session foo to foo. Then unset foo, so this if will not run if you F5 the page, then it will simply assume that the session foo is a-ok.

Then, you can use <?=$_SESSION['foo']?>

Olav Alexander Mjelde
Admin & Webmaster
 
np..
you could test on a "fresh" page, so you know it's not a typo messing up.

then you can try echo'ing out $_SESSION['variablename'] and $variablename, etc. to debug and check that thing are a-ok.

I'm soon off from wrk though, but I'll BBL in maybe 2-3hrs. I'll be idle though, as I'm working on a custom CMS in the administration-area. (just implemented FCKeditor ( hot diggety that's nice!).

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top