|
Posted by Tom on 05/25/07 17:45
"Tom" <tom@to.com> wrote in message news:f34hrr02hdu@news3.newsguy.com...
>
> "damezumari" <jannordgreen@gmail.com> wrote in message
> news:1180023096.751174.193800@p47g2000hsd.googlegroups.com...
> > On page a.php I have a form. When it is submitted, the $_POST
> > variables are validated on page b.php. If something is wrong, page
> > a.php is shown again with the already filled in field values in place
> > and an error message or two.
> >
> > How is this best done? That is, how do I put the already filled in
> > field values into the form so the user does not have to fill it in
> > again?
> >
> > Here is the way I do it. Please suggest better ways!
> >
> > At the top of a.php I have:
> > // when called for the first time, or after a successful update, fill
> > fields with the session variables
> > if (isset($_SESSION['error'])) // unsuccessful update, so keep the
> > changes that was made
> > {
> > unset($_SESSION['error']);
> > }
> > else
> > {
> > $_SESSION['parameters']['userfirstname'] =
> > $_SESSION['userfirstname'];
> > }
> >
> > In a.php I have something like this for each text field: (check boxes
> > and dropdown boxes are a bit different, but that is not essential for
> > this discussion)
> > <input type="text" name="userfirstname" value="<?=
> > $_SESSION['parameters']['userfirstname'];?>" />
> >
> > At the top of b.php I have:
> > $_SESSION['parameters'] = $_POST;
> >
> > If the validation in b.php goes sour:
> > $_SESSION['error'] = 'empty fields';
> > $_SESSION['message'] = '<b>Error: Some fields were missing! Please
> > rectify.</b>';
> > header('Location: ol_myprofile.php');
> > die();
> >
> > Regards,
> >
> > Jan Nordgreen
> >
>
> I'd probably just put the information in the form like this...
>
> <input type="text" name="userfirstname"
> value="<?=$_POST['userfirstname']?>">
>
>
> With regard to "<select>" form fields, I'd put the options into an array,
> then use a "for" loop to test is the submitted information matched
>
> $array = array('1','2','3');
> $size=sizeof($array);
> <select name="option1">
> <?
> for($i = 0; $i < $size; $i++)
> {
> echo("<option value=\"" . $array[$i] .
> $select = ($_FORM['option1'] == $array[$i]) ? ' selected':'';
> echo($select . "\">");
> }
> ?>
>
> Tom
> --
Oops I cheated and copied the $_FORM variable name from his example for the
second part of my response. : ) In the above for loop that should be this
instead...
$select = ($_POST['option1'] == $array[$i]) ? ' selected':'';
Tom
--
Newsguy.com
90+ Days Retention
Higher levels of article completion
Supports 32 concurrent connections
Navigation:
[Reply to this message]
|