keeping form fields set
Moderator: General Moderators
keeping form fields set
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????
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????
- R4000
- Forum Contributor
- Posts: 168
- Joined: Wed Mar 08, 2006 12:50 pm
- Location: Cambridge, United Kingdom
okay, first rename form.htm into form.php
and change all your:
to:
(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:
and change all your:
Code: Select all
<input type="text" name="NAME">Code: Select all
<input type="text" name="NAME" value="<?php echo $_GET["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:
Code: Select all
header("Location: form.htm");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)- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
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'.
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'.
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>- jayshields
- DevNet Resident
- Posts: 1912
- Joined: Mon Aug 22, 2005 12:11 pm
- Location: Leeds/Manchester, England
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.
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.
Last edited by jayshields on Thu Mar 09, 2006 1:54 pm, edited 1 time in total.
- R4000
- Forum Contributor
- Posts: 168
- Joined: Wed Mar 08, 2006 12:50 pm
- Location: Cambridge, United Kingdom
The only problem i have found with checking:
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.
Code: Select all
<input type="submit" name="submit" value="Submit!" />Code: Select all
if($_GET/POST["submit"]){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.
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?
<?
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?
Take a quick look and you'll see what I mean:
http://www.bertsparadise.com/ExpressionOfInterest3.htm
http://www.bertsparadise.com/ExpressionOfInterest3.htm
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
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
- R4000
- Forum Contributor
- Posts: 168
- Joined: Wed Mar 08, 2006 12:50 pm
- Location: Cambridge, United Kingdom
it should.
but your code is the same as:
and you sure your using _POST not _GET?
but your code is the same as:
Code: Select all
$_SESSION = $_POST;