$i = 0; foreach ($_POST as $value) { if (empty($value)) { $i++; } } # AMT = the amount of filled posts. for example, in your example # $amt would equal 0, if nothing was posted, $amt would also be 0 $amt = count($_POST) - $i;
$i = 0; foreach ($_POST as $value) { if (empty($value)) { $i++; } } # AMT = the amount of filled posts. for example, in your example # $amt would equal 0, if nothing was posted, $amt would also be 0 $amt = count($_POST) - $i;
That last line is moot. An empty POST array won't increment $i, hence it will be 0.
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.
I recently posted a variation of this in response to a separate but similar question:
PHP Code:
if(isset($_POST['submit'])){
$blank_inputs=0;
$test_array=array_count_values($_POST); //creates a test array
//the keys of the test array are set as the individual values of the original array
//the values of the test array are set as the count of occurences of each value in the original array
//so, for instance, if our posted values looked like this:
// $_POST: ("cat", "dog", "rabbit", "cat", "moose")
//then array_count_values($_POST) would create an array like this:
// $test_array: ("cat"=>2, "dog"=>1, "rabbit"=>1, "moose"=>1)
//then, when you check $test_array["cats"] you would return the number 2
//using this same idea, we can check for blank values...
$blank_inputs=$test_array[""]; //checking the test array to for the count of occurences of "" within the $_POST array...
if($blank_inputs>0){ //display an error message and put the form back up for editing
print "Blank inputs: ".$blank_inputs."<br />\nThis was a failure!<br />\n";
}
else { // ...do whatever you need to do with a successful submission...
print "No blank input fields - this was a success!";
}
}
In its simplest form the check would just look like this:
Then you can script based on the value of $blank_inputs.
I'm a relative PHP novice though. There might be better ways. For example, I don't know if this code is any faster than what has been posted so far. It may or may not be optimal but for my brain it's a bit easier to remember and use.
Presumably to avoid processing an incomplete form, but I am only making an educated guess at this.
Presumably yes, but in that case I'm not sure why this is necessary. Seems better to check required fields individually (then you can let them know which one is missing). Also I usually stick a:
Presumably yes, but in that case I'm not sure why this is necessary. Seems better to check required fields individually (then you can let them know which one is missing). Also I usually stick a:
Presumably yes, but in that case I'm not sure why this is necessary. Seems better to check required fields individually (then you can let them know which one is missing). Also I usually stick a:
somewhere in my code so that I know that my form actually has been submitted.
In my ~8 odd years of php programming I've never had to do this, so I'm wondering if it's not necessary or if I've just been doing things wrong
I suppose the merit of this approach depends to some extent on whether or not any of your input fields are optional and how you plan to notify the user of the missing data. If EVERYTHING is required then you could just load up the form again pre-filled with existing values but showing a generic "must complete all fields" error message to the user.
Anyway, if nothing else you could still use this check to see if looping through required values is even necessary. If everything is filled in you can skip a lot of switch cases or if/else statements. That might save some processing. I'm not really sure though. I'm garbage at code optimisation.
Also, if blank values exist you should be able to create an array of their keys like so (untested code found here):
That would help with your error message output. Just run the function array_search_values( "", $_POST); and handle errors based on that instead of any of the above? Dunno, just brain storming here... I always prefer simplicity where possible.