View Full Version : php array taking 2 form submits to work

06-01-2007, 11:08 PM
Hello and thank you in advance for taking a look at this.

This form handling script uses php redux to send the variables to itself.

I've got this little array setup so that if someone forgets to enter a required field, it will only show the fields missed instead of throwing the whole form back on screeen.

However, it takes 2 erroneous submissions to display all of them!

If the form is submitted with none of these fields filled in,
it simply returns the email field. Then when that is filled
and submitted, it shows all three (including the email again!).

here is the code:

if (!eregi("^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$",stripslashes(trim($_POST['emailaddress'])))) {
$form_errors["email"] = 'Please enter a valid Email Address: <input type="text" size="35" name="emailaddress" /><br />';
if (!isset($_POST['name'])){
$form_errors["name"] = 'Please fill in your name: <input type="text" size="45" name="name" /><br />';
if (!isset($_POST['phone'])){
$form_errors["phone"] = 'Enter a valid phone number: <input type="text" size="25" name="phone" /><br />';

if (is_array($form_errors)) {
while(list($key, $val) = each($form_errors)) {
echo $val;
echo "<br />\n";
echo '<input type="submit" name="action" value="send" style="border:1px solid #888; background:#e0e0e0; margin-right:300px;" />';
} else {
do other stuff...

See it in (broken) action here: http://www.elaborateforms.com/app_beta2.php

Again thank you in advance,

06-02-2007, 12:19 AM
At a glance it seems like your "if (!isset($_POST['name']))" condition isn't quite what you want-- That variable will exist and return true even if the user doesn't enter anything in the field.

There's other stuff happening but that's what jumped out at me.

Just curious-- why are you doing the form this way? IMO it's not intuitive to make fields disappear if they were filled in correctly-- maybe it's because it's not a common web form behavior...