• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

Why is my form validation only working on text fields?

Hi, I am trying to make it to where if any of the form fields are left without a value it does not process.  but so far its only working on text fields. Here for example:

http://www.auroriella.com/new_product.php

it only gives an error message if I leave name, content, and color blank (they are the only text fields).  everything else makes it past the validation.  Anyone suggestions?

// If there were errors --> display_errors()
function validate_fields() {
	global $errors;
	if(isset($_POST['submit'])) {
		$errors = array();
		// Form Validation
		$required_fields = array();
			foreach($_POST as $key => $value){
			$required_fields[] = $key;
		}
		foreach($required_fields as $fieldname) { // Checks if there was input
			if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) {
				// Errors occurred
				$errors[] = $fieldname;
				display_errors();
			}
		}
	}
}
// Displays the errors
// Determines if there was more than one error and adjusts message
function display_errors() {
	global $errors, $error;
	if(!empty($errors)) { // If there was only one error
			if(count($errors) > 1) {
				$error = "There were " . count($errors) . " errors in the form.<br />";
				$error .= "Please review the following fields:<br />";
				foreach($errors as $errored) {
					$error .= "<span> - " . $errored . "</span><br />";
				}
			}
			else { // If there were multiple errors
				$error = "There was 1 error in the form.<br />";
				$error .= "Please review the following field:<br />";
				$error .= " - " . $errors[0] . "<br />";
			}
	}
}

Open in new window

0
FairyBusiness
Asked:
FairyBusiness
  • 5
  • 4
1 Solution
 
cfEngineersCommented:
when using checkboxes, the fields do not get sent if they are not checked.

So you have to check for example the existance of metal_id etc...

to test this, change your method to get in the form you will see all your fields in the query_string

0
 
FairyBusinessAuthor Commented:
Well, if I change my method to get in the form, I have to go back and change all my function so use $_GET not $_POST right?? ..............
0
 
cfEngineersCommented:
just as a test, to see which fields are actually getting sent.

But the point is any fields you want to check for, that are checkboxes or radio buttons, need to be know ahead of time in the php they will not work dynamically.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
cfEngineersCommented:
foreach($required_fields as $fieldname) { // Checks if there was input
                  if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) {
                        // Errors occurred
                        $errors[] = $fieldname;
                        display_errors();
                  }
            }

// check metal_id
if(!isset($_POST['metal_id']) || (empty($_POST['metal_id']) && !is_numeric($_POST['metal_id']))) {
                        // Errors occurred
                        $errors[] = 'metal_id';
                        display_errors();
                  }
0
 
FairyBusinessAuthor Commented:
yeah that last way you posted works but I have too many check boxes to do that for. ..
0
 
FairyBusinessAuthor Commented:
I dont think I need to use the get because I have already checked and verified that everything gets sent to the database
0
 
cfEngineersCommented:
get was just to look in the url and see which fieldnames get sent.

Anyways,
you can create an array of the fieldnames you want to check for. and use a similar foreach to handle them.
0
 
cfEngineersCommented:
// set required fields here
$required_fields = array ("metal_id","featured","stock");
foreach($required_fields as $fieldname) { // Checks if there was input
                  if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && !is_numeric($_POST[$fieldname]))) {
                        // Errors occurred
                        $errors[] = $fieldname;
                        display_errors();
                  }
            }
0
 
FairyBusinessAuthor Commented:
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now