[thelist] Forms and passing variables

Tom Dell'Aringa pixelmech at yahoo.com
Mon Jan 27 21:10:25 CST 2003


--- Rachel Cunliffe <r.cunliffe at auckland.ac.nz> wrote:
> I have a form with many fields in it, which processes it using php
> on
> another page.  If there are required fields which are missing, it
> goes
> back to the previous page (using header location) with an error
> code
> appended to the file name, e.g. index.php?error=1.  My problem is
> that
> the form does not remember what was entered in the first time (even
> though I am using echo $variable; in the text attribute.. Obviously
> because while the form variables were passed onto the second page,
> they
> have not been passed back to the first page.
>
> Is there a way of passing it back, without having to do
> index.php?error=1&variable1=foo&variable2=bar etc?  A simple way of
> passing them back would be greatly appreciated.. Or do I really
> *have*
> to recode the way the form is processed and do it all on the same
> page?

Rachel, a good way to handle this is to have ONE page, not two, do
all the work. You handle this by testing the value of your submit
button. If your submit value is "Send Form" then you can do something
like this:

$submit = $_POST["submit"]; //assuming the name attr. of submit is
'submit'

if($submit == "Send Form")
{
  //you know you are coming from the form submission
  // get all your POST vars
  // etc
}
else
{
  you just came here from a link or direct page load
}

If you are coming from the form, you get all your variables from the
form of course, and you process the form. If there is an error in the
form you have all your post variables. You can head back down to the
form and reload it with the info the user entered if there is an
error like this:

if($submit == "Send Form")
{
  //you know you are coming from the form submission
  // get all your POST vars
  if(!some error)
  {
    // no error...do header("Location: somewhere");
  }
   // otherwise the page will continue loading WITH the post vars you
got, into the form.
}

If you are in the else condition (as mentioned above, no form
submission) the page will load normally without the POST variables of
course (you never entered the if condition).

There are times to use 2 pages for form submissions etc, and times
for 1 page - this is a good time to use 1!

Hope this is clear and helps..

Tom

=====
var me = tom.pixelmech.webDeveloper();

http://www.pixelmech.com/
http://www.maccaws.com/
[Making A Commercial Case for Adopting Web Standards]

__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com



More information about the thelist mailing list