Jump to content

[SOLVED] Form not validating...help!!


jamichelli

Recommended Posts

I have several forms that I am using on different pages of my website.  They all use the same validation.php file to validate, the same coding to run the validation, the same rules...customized for the field names, of course...and I used the same basic template for each page and just customize the content and field names to fit the specific page theme.  As far as I can tell, there is no reason why all of the forms on my various pages validate except one.  Is there something specific that I should be looking for? Here are the parts of the code that I think are relevant:

1.  The rules:

<?php

$errors = array(); // set the errors array to empty, by default

$fields = array(); // stores the field values

$success_message = "";

 

if (isset($_POST['submit']))

{

  // import the validation library

  require("validation.php");

 

  $rules = array(); // stores the validation rules

 

  // standard form fields

  $rules[] = "required,full_name,You did not enter your name!";

 

  $errors = validateFields($_POST, $rules);

 

  // if there were errors, re-populate the form fields

  if (!empty($errors))

  { 

    $fields = $_POST;

  }

 

  // no errors! redirect the user to the thankyou page (or whatever)

  else

  {

    $message = "All fields have been validated successfully!";

   

    // here you would either email the form contents to someone or store it in a database.

    // To redirect to a "thankyou" page, you'd just do this:

    // header("Location: thanks.php");

  }

}

?>

 

2.  Where the error message goes:

<?php

   

    // if $errors is not empty, the form must have failed one or more validation

    // tests. Loop through each and display them on the page for the user

    if (!empty($errors))

    {

      echo "<div class='error' style='width:100%;'>Please fix your errors:\n";

      foreach ($errors as $error)

        echo "<li>$error</li>\n";

   

      echo "</ul></div>";

    }

   

    if (!empty($message))

    {

      echo "<div class='notify'>$success_message</div>";

    }

    ?>

3.  The form field:

  <tr> 

    <td colspan="2" class="labelcell"><label for="full_name">Full name (required)</label></td>

    <td colspan="2" class="fieldcell"> <input type="text" name="full_name" id="full_name" tabindex="1" value="<?=$fields['full_name']?>" /></td>

</tr>

 

This validates and returns the errors, if present, or submits if there are no errors present...for all except one form...?!?

 

Any help on what I need to look for would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/153238-solved-form-not-validatinghelp/
Share on other sites

The above is what is not working.  Following is what is working:

 

1. The rules:

<?php

 

$errors = array(); // set the errors array to empty, by default

$fields = array(); // stores the field values

$success_message = "";

 

if (isset($_POST['submit']))

{

  // import the validation library

  require("validation.php");

 

  $rules = array(); // stores the validation rules

 

  // standard form fields

  $rules[] = "required,subject_address1,You did not enter your address!";

 

  $errors = validateFields($_POST, $rules);

 

  // if there were errors, re-populate the form fields

  if (!empty($errors))

  { 

    $fields = $_POST;

  }

 

  // no errors! redirect the user to the thankyou page (or whatever)

  else

  {

    $message = "All fields have been validated successfully!";

   

    // here you would either email the form contents to someone or store it in a database.

    // To redirect to a "thankyou" page, you'd just do this:

    // header("Location: thanks.php");

  }

}

?>

 

2.  Where the error message goes:

<?php

   

    // if $errors is not empty, the form must have failed one or more validation

    // tests. Loop through each and display them on the page for the user

    if (!empty($errors))

    {

      echo "<div class='error' style='width:100%;'>Please fix your errors:\n";

      foreach ($errors as $error)

        echo "<li>$error</li>\n";

   

      echo "</ul></div>";

    }

   

    if (!empty($message))

    {

      echo "<div class='notify'>$success_message</div>";

    }

    ?>

3. The form fields:

  <tr> 

    <td colspan="2" class="labelcell"><label for="subject_address1">Property Address Line 1 (required)</label></td>

    <td colspan="2" class="fieldcell"> <input type="text" name="subject_address1" id="subject_address1" tabindex="3" value="<?=$fields['subject_address1']?>" /></td>

  </tr> 

 

Hope this helps.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.