Skip to main content
Known Participant
May 12, 2011
Question

registration form submission

  • May 12, 2011
  • 1 reply
  • 1437 views

i have a registration form, which checks for errors before the form is submitted.  i was told then to use an if else statement to submit the form on the same page, but i would prefer to have the user go to a confirmation page instead.  i have the form submitting to itself to check for errors.  any ideas as to how i can do this?

This topic has been closed for replies.

1 reply

nickentin
Participating Frequently
May 12, 2011

Should be simple enough.

<?php

     if(isset($_POST['submit'])) // when your submit button has name="submit"

     {

          // code for your confirmation page

     }

     else

     {

          // code for your page with the form

     }

?>

Nick

Known Participant
May 13, 2011

here is how i modified the code:

<?php
     if(isset($_POST['register']) && empty($errors)) // when your submit button has name="submit"
     {
         echo "the form will have been submitted!";
     }
     else
     {
         echo "registration form displayed here...";
     }
?>

i want to make sure that the user did not leave any empty fields.  but i get "  the form will have been submitted" when i submit it with empty fields....

Known Participant
May 15, 2011

Ahh, so the problem is that you are not checking whether each field is filled, but whether atleast one field is filled.  Here's another solution:

<?php

    $valid = true;

    foreach ($errors as &$value)

    {
        if ($value == "")

        {

             $valid = false;

        }
    }

    if(isset($_POST['register']) && $valid)
    {
        echo "the form will have been submitted!";
    }
    else
    {
        echo "registration form displayed here...";
    }
?>

Nick


ok, so here is how i modified the code:

<?php

    $valid = true;

    foreach ($errors as $value)

    {
        if ($value == "")

        {

             $valid = false;

        }
    }

    if(isset($_POST['register']) && $valid)
    {
        echo "the form will have been submitted";
    }
    else
    {
        echo "registration form with errors displayed here...";
    }
?>

it did not like the & sign before the $value.  when i submit the form with empty fields, i still get "   the form will have been submitted"

here is what i have for the error-checking array:

if (array_key_exists('register',$_POST)){
    // Fields that are on form
    $expected = array('FName', 'LName', 'phone', 'cell', 'street-address', 'state', 'zip', 'email', 'emergecnycontactname', 'pphonenumber', 'relationship', 'aphonenumber');
    // Set required fields
    $required = array('FName', 'LName', 'phone', 'cell', 'street-address', 'state', 'zip', 'email', 'emergencycontactname', 'pphonenumber', 'relationship');
    // Initialize array for errors
    $errors = array();
   
    foreach ($_POST as $field => $value){
        // Assign to $temp and trim spaces if not array
        $temp = is_array($value) ? $value : trim($value);
        // If field is empty and required, tag onto $errors array
        if (empty($temp) && in_array($field, $required)) {
            array_push($errors, $field);
        }
    }