?
Solved

Why is my form validation only working on text fields?

Posted on 2011-05-03
9
Medium Priority
?
212 Views
Last Modified: 2012-05-11
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
Comment
Question by:FairyBusiness
  • 5
  • 4
9 Comments
 
LVL 6

Expert Comment

by:cfEngineers
ID: 35514988
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
 

Author Comment

by:FairyBusiness
ID: 35515013
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
 
LVL 6

Expert Comment

by:cfEngineers
ID: 35515087
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:cfEngineers
ID: 35515191
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
 

Author Comment

by:FairyBusiness
ID: 35515237
yeah that last way you posted works but I have too many check boxes to do that for. ..
0
 

Author Comment

by:FairyBusiness
ID: 35515252
I dont think I need to use the get because I have already checked and verified that everything gets sent to the database
0
 
LVL 6

Expert Comment

by:cfEngineers
ID: 35515275
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
 
LVL 6

Accepted Solution

by:
cfEngineers earned 2000 total points
ID: 35515320
// 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
 

Author Closing Comment

by:FairyBusiness
ID: 35515344
Thanks!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question