How to call my form by a name in php?

Hi, I was checking to see whether or not a form as a certain name (because then it should have extra validation) and it was working!  but then I did something and now its not!  If you click create:

http://auroriella.com/new_product.php

you will see it only displays three fields that are missing values when it should display all of them.

I tested this and it worked before. . .

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();
			}
		}
		if(isset($_POST['products'])) {
		echo "hello";
			// set required fields here
			$required_fields = array("metal_id","featured","stock","materials");
			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();
				}
			}
		}
	}
}

Open in new window


I guess I changed something because now its back to only display an error message for the text fields.  Can someone tell me whats going on?
FairyBusinessAsked:
Who is Participating?
 
Marco GasiFreelancerCommented:
When php check $_POST array, it sees tyhe name of submit inputs not the form's names. I suggest to rename the input submit this way

<input type='submit' name='submit_products' value='Create' />

and then check values this way:
if(isset($_POST['submit_products'])) {
		echo "hello";
			// set required fields here
			$required_fields = array("metal_id","featured","stock","materials");
			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();
				}
			}
		}

Open in new window


Hope (and think) this eill work.

Cheers
0
 
Marco GasiFreelancerCommented:
I shows only three errors because it shows only fields that are submitted with an empty value: checkboxes and file input type, if empty, are not submitted at all so they don't generate nor errors nor warnings nor notices.

Cheers
0
 
FairyBusinessAuthor Commented:
so thats why I put them manually into the array:

if(isset($_POST['products'])) {
		echo "hello";
			// set required fields here
			$required_fields = array("metal_id","featured","stock","materials");
			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();
				}
			}
		}

Open in new window


It works when I take away the if(isset($_POST['products'])) {
but then it adds that to all of the other forms in the website and I don't want that.  So I named the form products just to target those which deal with products.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
Marco GasiFreelancerCommented:
Sorry for stupid question, FairyBusiness, but 'products' is the form name or the submit input name?
0
 
FairyBusinessAuthor Commented:
I put it has the form name like this:

<form name="products" enctype="multipart/form-data"  method="post" action="new_product.php">

0
 
FairyBusinessAuthor Commented:
Thanks!
0
 
Marco GasiFreelancerCommented:
Glad to help you! This is not the first time I see a your question, but I've never been able to help :-) Bye
0
 
FairyBusinessAuthor Commented:
Oh, well thanks for the help :)  I always appreciate it!
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.

All Courses

From novice to tech pro — start learning today.