Topic: PHP Validation code help (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=30626" title="Pages that link to Topic: PHP Validation code help (Page 1 of 1)" rel="nofollow" >Topic: PHP Validation code help <span class="small">(Page 1 of 1)</span>\

 
Hustluz
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2003

posted posted 10-30-2008 21:38

I have a PHP code validating that someone should work but dosent work correctly

my code is ass follows

code:
if (isset($_POST['submit'])) {
			$errors = array();

		//Form Validation
		$required_fields = array('forum_name', 'position', 'visible');
		foreach($required_fields as $fieldname){
			if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)){
				$errors[] = $fieldname;
			}
		}
		
		$fields_with_lengths = array('forum_name' => 225);
		foreach($fields_with_lengths as $fieldname => $maxlength ) {
			if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) { $errors[] = $fieldname; }
		}



1. the first party checks to see if the form is submited
2. the next step would be to create an array that would store the fieldnames with errors
3. the problem occurs when with the second part of the if(isset) statement which says if the filednames is not set or if the fields name is emtpy and not equal to zero.
4. the probelm is when i delete the input field to make sure someone is not passing in a empty imput it accepts it when it should reject it because it is empty. it works when i dont add the and statement to check and see if its not equal to zero. but when its in there i wont pick up on the fact that its empty and send it to the $errors array and reject the update?


please anyone who can help me with this topic your help will be greatly appreciated. thank you in advance

twItch^
Maniac (V) Mad Scientist

From: Denver, CO, USA
Insane since: Aug 2000

posted posted 10-30-2008 22:39

Well, you could trim that big IF statement down to this:

code:
if($_POST[$fieldname] == '')



That should catch all your cases there. isset() doesn't capture blank strings, which means that

code:
$var='';
if(!isset($var)) echo ("This variable is not set");



"This variable is not set" would not be printed, so isset() has limited use for you here. In a $_POST array, all values from the form are passed, so even if it's a blank return, it'll still validate as true for the purposes of your query.

-S

Nathus
Bipolar (III) Inmate

From: Minnesota
Insane since: Aug 2003

posted posted 10-30-2008 22:41

Change it to $_POST[$fieldname] != "0";

By comparing the empty input which is a string to an integer, PHP is casting the empty string "" to the integer 0. The change will make sure that the input, which is a string anyway will compare to the string 0.

Essentially, you were doing the same thing as (int)$_POST[$fieldname] != 0;

Which, as you can see by running a simple test.

code:
echo (int)"";



Will give you the output of 0.

Nathus
Bipolar (III) Inmate

From: Minnesota
Insane since: Aug 2003

posted posted 10-30-2008 22:44

My code shows why it wasn't working the way you expected.

twItch^'s recommendation is the best way to do it.

Hustluz
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2003

posted posted 10-30-2008 23:08

Thank you very much for your help Nathus this works for me right now as a temporary fix

as for twItch^ thank you to as well. i will use Nathus fix for a quck temp fix but as he agress with you as to the best solution before i apply it i would like to make sure that i am understanding you correctly to try and avoid more problems..

you are say to me that

code:
if($_POST[$fieldname] == '')



accomplishes the same thing as

code:
if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != "0"))



just more efficiently which means i could just substitute one for the other and they will accomplish the same thing?

Hustluz
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2003

posted posted 10-30-2008 23:57

Okay i know im a noob and sound stupid to all you vets lol...

but thank you no further responses needed for this question i figured out and trimming that if statement works very nicely thank you both very much.. im sure i will need help again lol



Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu