Prev: [PHP]
Next: curl
From: David Mehler on
Hello,
I've got a form with several required fields of different types. I
want to have the php script process it only when all the required
fields are present, and to redisplay the form with filled in values on
failure so the user won't have to fill out the whole thing again.
One of my required fields is a text input field called name. If it's
not filled out the form displayed will show this:

<input type="text" name="name" id="name" size="50" value="<?php
echo($name); ?>" /> <br />

Note, I've got $_POST* variable processing before this so am assigning
that processing to short variables.
If that field is filled out, but another required one is not that form
field will fill in the value entered for the name field.
This is working for my text input fields, but not for either select
boxes or textareas. Here's the textarea also a required field:

<textarea name="description" id="description" cols="50" rows="10"
value="<?php echo($description); ?>"></textarea>

What this does, if a user fills out this field, but misses another, it
should echo the value of what was originally submitted. It is not
doing this. Same for my select boxes, here's one:

<select name="type" id="type" value="<?php echo($type); ?>">
<option value="0" selected="selected">-- select type --</option>
<option value="meeting"> - Meeting - </option>
<option value="event"> - Event - </option>
</select>

I'd also like for any not entered required fields to have an error box
around them, I've got a css class to handle this, but am not sure how
to tie it in to the fields since any one of the required fields could
not be filled in.
I'd appreciate any help.
Thanks.
Dave.
From: Shreyas Agasthya on
David,

If I understand your problem/issue here, you are talking about something
called 'sticky forms'.
This means -
(i) the form references itself.
(ii) that the form knows what the previous data was when it encounters any
validation issues.

You achieve (i) and (ii) by re-submitting the form with the usage of a
superglobal variable called $_SERVER['PHP_SELF'].

<form method='POST' action ="<php echo $_SERVER['PHP_SELF'] ?>" >

Regards,
Shreyas


On Sun, Jul 4, 2010 at 11:27 PM, David Mehler <dave.mehler(a)gmail.com> wrote:

> Hello,
> I've got a form with several required fields of different types. I
> want to have the php script process it only when all the required
> fields are present, and to redisplay the form with filled in values on
> failure so the user won't have to fill out the whole thing again.
> One of my required fields is a text input field called name. If it's
> not filled out the form displayed will show this:
>
> <input type="text" name="name" id="name" size="50" value="<?php
> echo($name); ?>" /> <br />
>
> Note, I've got $_POST* variable processing before this so am assigning
> that processing to short variables.
> If that field is filled out, but another required one is not that form
> field will fill in the value entered for the name field.
> This is working for my text input fields, but not for either select
> boxes or textareas. Here's the textarea also a required field:
>
> <textarea name="description" id="description" cols="50" rows="10"
> value="<?php echo($description); ?>"></textarea>
>
> What this does, if a user fills out this field, but misses another, it
> should echo the value of what was originally submitted. It is not
> doing this. Same for my select boxes, here's one:
>
> <select name="type" id="type" value="<?php echo($type); ?>">
> <option value="0" selected="selected">-- select type --</option>
> <option value="meeting"> - Meeting - </option>
> <option value="event"> - Event - </option>
> </select>
>
> I'd also like for any not entered required fields to have an error box
> around them, I've got a css class to handle this, but am not sure how
> to tie it in to the fields since any one of the required fields could
> not be filled in.
> I'd appreciate any help.
> Thanks.
> Dave.
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


--
Regards,
Shreyas Agasthya
From: Paul M Foster on
On Sun, Jul 04, 2010 at 01:57:01PM -0400, David Mehler wrote:

> Hello,
> I've got a form with several required fields of different types. I
> want to have the php script process it only when all the required
> fields are present, and to redisplay the form with filled in values on
> failure so the user won't have to fill out the whole thing again.
> One of my required fields is a text input field called name. If it's
> not filled out the form displayed will show this:
>
> <input type="text" name="name" id="name" size="50" value="<?php
> echo($name); ?>" /> <br />
>
> Note, I've got $_POST* variable processing before this so am assigning
> that processing to short variables.
> If that field is filled out, but another required one is not that form
> field will fill in the value entered for the name field.
> This is working for my text input fields, but not for either select
> boxes or textareas. Here's the textarea also a required field:
>
> <textarea name="description" id="description" cols="50" rows="10"
> value="<?php echo($description); ?>"></textarea>

Textarea fields don't work this way. To display the prior value, you
have to do this:

<textarea name="description><?php echo $description; ?></textarea>

>
> What this does, if a user fills out this field, but misses another, it
> should echo the value of what was originally submitted. It is not
> doing this. Same for my select boxes, here's one:
>
> <select name="type" id="type" value="<?php echo($type); ?>">
> <option value="0" selected="selected">-- select type --</option>
> <option value="meeting"> - Meeting - </option>
> <option value="event"> - Event - </option>
> </select>

The "value" attribute of a select field won't do this for you. You have
to actually set up each option with an either/or choice, like this:

<option value="0" <?php if ($type == 'meeting') echo 'selected="selected"';
?>> - Meeting - </option>

Since doing this is pretty tedious, I use a function here instead:

function set_selected($fieldname, $value)
{
if ($_POST[$fieldname] == $value)
echo 'selected="selected"';
}

And then

<option value="meeting" <?php set_selected('type', 'meeting');
?>>Meeting</option>

HTH,

Paul

--
Paul M. Foster
From: David Mehler on
Hello everyone,
Thanks for your suggestions.
For my variable in the value area of the text input field I enter

value="<?php echo $name"; ?>

Prior to this I assign the variable $name to:

$name = stripslashes($_POST['name']);

I hope this is correct.
Sticky forms sounds exactly what i'm looking for. I've changed my
action attribute to

<?php echo $_SERVER['PHP_SELF']; ?>

The first thing I do once the page is loaded is check whether or not
submit is set, if it is not I display the form, which is in a function
call. If submit is set I want to begtin validation, so i'm deciding to
merge my two files in to one, I like this better. My question is say
for example the name text field is not filled out but all the other
required fields are how do I get the form to redisplay itself? I was
thinking a location redirect, but this doesn't sound right.
Thanks.
Dave.


On 7/4/10, Paul M Foster <paulf(a)quillandmouse.com> wrote:
> On Sun, Jul 04, 2010 at 01:57:01PM -0400, David Mehler wrote:
>
>> Hello,
>> I've got a form with several required fields of different types. I
>> want to have the php script process it only when all the required
>> fields are present, and to redisplay the form with filled in values on
>> failure so the user won't have to fill out the whole thing again.
>> One of my required fields is a text input field called name. If it's
>> not filled out the form displayed will show this:
>>
>> <input type="text" name="name" id="name" size="50" value="<?php
>> echo($name); ?>" /> <br />
>>
>> Note, I've got $_POST* variable processing before this so am assigning
>> that processing to short variables.
>> If that field is filled out, but another required one is not that form
>> field will fill in the value entered for the name field.
>> This is working for my text input fields, but not for either select
>> boxes or textareas. Here's the textarea also a required field:
>>
>> <textarea name="description" id="description" cols="50" rows="10"
>> value="<?php echo($description); ?>"></textarea>
>
> Textarea fields don't work this way. To display the prior value, you
> have to do this:
>
> <textarea name="description><?php echo $description; ?></textarea>
>
>>
>> What this does, if a user fills out this field, but misses another, it
>> should echo the value of what was originally submitted. It is not
>> doing this. Same for my select boxes, here's one:
>>
>> <select name="type" id="type" value="<?php echo($type); ?>">
>> <option value="0" selected="selected">-- select type --</option>
>> <option value="meeting"> - Meeting - </option>
>> <option value="event"> - Event - </option>
>> </select>
>
> The "value" attribute of a select field won't do this for you. You have
> to actually set up each option with an either/or choice, like this:
>
> <option value="0" <?php if ($type == 'meeting') echo 'selected="selected"';
> ?>> - Meeting - </option>
>
> Since doing this is pretty tedious, I use a function here instead:
>
> function set_selected($fieldname, $value)
> {
> if ($_POST[$fieldname] == $value)
> echo 'selected="selected"';
> }
>
> And then
>
> <option value="meeting" <?php set_selected('type', 'meeting');
> ?>>Meeting</option>
>
> HTH,
>
> Paul
>
> --
> Paul M. Foster
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
From: Ashley Sheridan on
On Sun, 2010-07-04 at 18:23 -0400, David Mehler wrote:

> Hello everyone,
> Thanks for your suggestions.
> For my variable in the value area of the text input field I enter
>
> value="<?php echo $name"; ?>
>
> Prior to this I assign the variable $name to:
>
> $name = stripslashes($_POST['name']);
>
> I hope this is correct.
> Sticky forms sounds exactly what i'm looking for. I've changed my
> action attribute to
>
> <?php echo $_SERVER['PHP_SELF']; ?>
>
> The first thing I do once the page is loaded is check whether or not
> submit is set, if it is not I display the form, which is in a function
> call. If submit is set I want to begtin validation, so i'm deciding to
> merge my two files in to one, I like this better. My question is say
> for example the name text field is not filled out but all the other
> required fields are how do I get the form to redisplay itself? I was
> thinking a location redirect, but this doesn't sound right.
> Thanks.
> Dave.
>
>
> On 7/4/10, Paul M Foster <paulf(a)quillandmouse.com> wrote:
> > On Sun, Jul 04, 2010 at 01:57:01PM -0400, David Mehler wrote:
> >
> >> Hello,
> >> I've got a form with several required fields of different types. I
> >> want to have the php script process it only when all the required
> >> fields are present, and to redisplay the form with filled in values on
> >> failure so the user won't have to fill out the whole thing again.
> >> One of my required fields is a text input field called name. If it's
> >> not filled out the form displayed will show this:
> >>
> >> <input type="text" name="name" id="name" size="50" value="<?php
> >> echo($name); ?>" /> <br />
> >>
> >> Note, I've got $_POST* variable processing before this so am assigning
> >> that processing to short variables.
> >> If that field is filled out, but another required one is not that form
> >> field will fill in the value entered for the name field.
> >> This is working for my text input fields, but not for either select
> >> boxes or textareas. Here's the textarea also a required field:
> >>
> >> <textarea name="description" id="description" cols="50" rows="10"
> >> value="<?php echo($description); ?>"></textarea>
> >
> > Textarea fields don't work this way. To display the prior value, you
> > have to do this:
> >
> > <textarea name="description><?php echo $description; ?></textarea>
> >
> >>
> >> What this does, if a user fills out this field, but misses another, it
> >> should echo the value of what was originally submitted. It is not
> >> doing this. Same for my select boxes, here's one:
> >>
> >> <select name="type" id="type" value="<?php echo($type); ?>">
> >> <option value="0" selected="selected">-- select type --</option>
> >> <option value="meeting"> - Meeting - </option>
> >> <option value="event"> - Event - </option>
> >> </select>
> >
> > The "value" attribute of a select field won't do this for you. You have
> > to actually set up each option with an either/or choice, like this:
> >
> > <option value="0" <?php if ($type == 'meeting') echo 'selected="selected"';
> > ?>> - Meeting - </option>
> >
> > Since doing this is pretty tedious, I use a function here instead:
> >
> > function set_selected($fieldname, $value)
> > {
> > if ($_POST[$fieldname] == $value)
> > echo 'selected="selected"';
> > }
> >
> > And then
> >
> > <option value="meeting" <?php set_selected('type', 'meeting');
> > ?>>Meeting</option>
> >
> > HTH,
> >
> > Paul
> >
> > --
> > Paul M. Foster
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>


$_SERVER['PHP_SELF'] is not to be trusted, and shouldn't be used as the
action of a form like this.
http://www.mc2design.com/blog/php_self-safe-alternatives explains it all
better than I can here, so it's worth a read, but it does list safe
alternatives.

One thing I do when creating sticky select lists is this:

$colours = array('red', 'green', 'blue', 'yellow', 'pink');

echo '<select name="colour">';
for($i=0; $i<count($colours); $i++)
{
$selected = (isset($_POST['colour']) && $_POST['colour'] ==
$i)?'selected="selected"':'';
echo "<option value=\"$i\" $selected>{$colours[$i]}</option>";
}
echo '</select>';

Basically, this uses PHP to not only output the list from an array
(which itself can be populated from a database maybe) and select the
right option if it exists in the $_POST array and matches the current
option in the loop that's being output.

Thanks,
Ash
http://www.ashleysheridan.co.uk


 |  Next  |  Last
Pages: 1 2
Prev: [PHP]
Next: curl