Page 1 of 2
keeping form fields set
Posted: Thu Mar 09, 2006 1:02 pm
by 5leander5
How can I ensue that if a user does not fill in a certain form field, that when I redirect the user back to the form, that the fields which he had filled in originally are still filled in? I presume that you might have to send the variable back to the form????
For example,
$email = $_POST['Email'];
if (empty($email)) header( "Location:
http://www.bertsparadise.com/form.htm" );
If any other fields were filled out by the user, that data would be lost by reloading the form.htm.
Any suggestions please????
Posted: Thu Mar 09, 2006 1:43 pm
by R4000
okay, first rename form.htm into form.php
and change all your:
to:
Code: Select all
<input type="text" name="NAME" value="<?php echo $_GET["NAME"]; ?>">
(change NAME into the form element name)
That'll get the form text input boxes done. for option/select you will need to build your own function to select the correct item.
Then just mod your:
to be:
Code: Select all
header("Location: form.php?".$_SERVER['query_string'])[/p]
Correct me if im wrong, but that should work.
This is no use for one page php scripts, your form and program have to be seperate.
(DONT FLAME ME FOR POSTING THIS WAAAY AFTER AN ANSWER HAS PROBERLY BEEN GIVEN, i typed this before i left, and came back an hour later and forgot i had'nt posted it)
Posted: Thu Mar 09, 2006 1:49 pm
by jayshields
Well, with forms handled by PHP I always have them on the same script.
Have if(isset($_POST['submit'])) at the top, if they submitted, validate the fields and what not.
Then in the HTML form field just put value="<?php if(isset($_POST['inputname'])) echo $_POST['inputname']; ?>".
There's loads of other ways to do it, that's just the way I do it.
By the way, it's referred to as a 'sticky form'.
Posted: Thu Mar 09, 2006 1:50 pm
by matthijs
Don't forget to escape the data:
Code: Select all
<?php
if (isset($_POST['submit'])) {
// process stuff
}
<div><label for="email">Email: (*)</label>
<input type="text" class="text" name="email" id="email" size="25"
value="<?php echo (isset($_POST['email'])) ? htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8') : ""; ?>" />
</div>
Posted: Thu Mar 09, 2006 1:51 pm
by jayshields
What's the point in that?
The ternary operator is misused in a sense, you haven't escaped any data and if the user is entering some non-alphanumeric characters it's going to change them, forcing the user to change them back? Unless htmlentities() does sometihng I'm not aware of...?
I would presume he/she would escape the data where I previously said validate and what not.
Posted: Thu Mar 09, 2006 1:52 pm
by R4000
The only problem i have found with checking:
Code: Select all
<input type="submit" name="submit" value="Submit!" />
with:
Is that if the user press enter on a form element, instead of clicking the button.
I have experianced where the form doesnt send the 'submit' data.
It might sound a bit wierd... but i've had it happen WAAAY to many times.
Posted: Thu Mar 09, 2006 2:30 pm
by matthijs
It's just something I'm used to: always escape any output. In this case output to html.. Of course when you're receiving the data you should do input validation.
R4000, that's an interesting point. I've not yet had any problems, but i'll certainly check it out.
Posted: Thu Mar 09, 2006 2:56 pm
by 5leander5
This is the start of my php script:
<?
session_start() ;
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}
In my form, I have the following test
<input name="Name" type="text" id="<?php if(isset($_SESSION['Name'])){
echo $_SESSION['Name']; }?>" tabindex="2" size="29">
However, it is not showing a name if I enter it in the form initially.
Can someone see where I'm going wrong here?
Posted: Thu Mar 09, 2006 3:01 pm
by feyd
you're putting it in the id attribute? why not the value attribute?
Posted: Thu Mar 09, 2006 3:04 pm
by 5leander5
When I use value (instead of id) the actual text <?php if(isset($_SESSION['Name'])){
echo $_SESSION['Name']; }?>" appears in the text field on the screen.
Any ideas?
Posted: Thu Mar 09, 2006 3:08 pm
by feyd
isn't that what you wanted?
Posted: Thu Mar 09, 2006 3:10 pm
by 5leander5
Posted: Thu Mar 09, 2006 3:32 pm
by R4000
you want it in the value...
i'm guessing that you want it to remember the users name if they have to go back.
if so, you want it in the value.
and you should make your page a .php not .htm then it might work

Posted: Thu Mar 09, 2006 3:49 pm
by 5leander5
I changed it to a .php file and it clears up that anyway. At least the syntax is now recognised.
However, the field is now blank even if I fill it out the first time. Can you see if this is correct in the php script:
<?
session_start() ;
foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
Will this set the $_SESSION['Name'] variable if there is a value in that variable
Posted: Thu Mar 09, 2006 3:56 pm
by R4000
it should.
but your code is the same as:
and you sure your using _POST not _GET?