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

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?
0
FairyBusiness
Asked:
FairyBusiness
  • 4
  • 4
1 Solution
 
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
 
Marco GasiFreelancerCommented:
Sorry for stupid question, FairyBusiness, but 'products' is the form name or the submit input name?
0
Technology Partners: 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!

 
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
 
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
 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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