Re: [PHP] if elseif elseif elseif....
PJ wrote: This is probably a mysql question, but their list is rather dull - I think they don't appreciate my humor. Beside this list is fun ... and informative. Anyway, I can't figure this out. I am trying to verify inputs on a form and even if I have all the required fields right, I still get the error that there are empty obligatory fields. This is what I am using... but I'm open to anything that works or is more efficient: if (strlen($_POST[titleIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[first_nameIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[publisherIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[copyrightIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[ISBNIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[languageIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[categoriesIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } I've tried closing with else ($obligatoryFiieldNot Present = 0); end; but that give me a blank page - haven't figure out how to check for parsing or scripting errors Thanks for any suggestions. Generally, the approach I take is as follows: $errors = array(); if (empty($_POST['name'])) array_push($errors, 'A name was not entered.'); if (empty($_POST['address'])) array_push($errors, 'An address was no supplied.'); if (empty($_POST['gender']) || ($_POST['gender'] != 'M' $_POST['gender'] != 'F')) array_push($errors, 'A gender was not supplied.'); And then you can check that all form fields were correctly filled in by checking for: count($errors) == 0 If that is not true, simply output the contents of the $errors array appropriately. The advantages of doing it like this are: 1. You get full details on why the form was not filled out, rather than simply telling users The form was not filled in and not telling them what fields they missed or what was incorrectly filled out. 2. You can set multiple criteria over and above simply checking for a value, e.g., ensuring that the gender field is either M or F and not an invalid value (in practice this will probably be a radio or drop list to ensure correct values, but this method ensures that you can do value sanity checking as well for things like dates etc) Hope that helps! - Naz. -- ?? ? Web: www.mrnaz.com Ph: +61 400 460 662
Re: [PHP] if elseif elseif elseif....
On Sun, Mar 08, 2009 at 03:03:33PM +1100, Naz wrote: Generally, the approach I take is as follows: $errors = array(); if (empty($_POST['name'])) array_push($errors, 'A name was not entered.'); if (empty($_POST['address'])) array_push($errors, 'An address was no supplied.'); if (empty($_POST['gender']) || ($_POST['gender'] != 'M' $_POST['gender'] != 'F')) array_push($errors, 'A gender was not supplied.'); And then you can check that all form fields were correctly filled in by checking for: count($errors) == 0 If that is not true, simply output the contents of the $errors array appropriately. The advantages of doing it like this are: 1. You get full details on why the form was not filled out, rather than simply telling users The form was not filled in and not telling them what fields they missed or what was incorrectly filled out. 2. You can set multiple criteria over and above simply checking for a value, e.g., ensuring that the gender field is either M or F and not an invalid value (in practice this will probably be a radio or drop list to ensure correct values, but this method ensures that you can do value sanity checking as well for things like dates etc) From the php.net manual for array_push(): Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function. But that's the only change I would make. Overall, I completely agree with this philosophy. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
Daniel Brown-7 wrote: On Wed, Mar 4, 2009 at 20:10, Al n...@ridersite.org wrote: $obligatoryFieldNotPresent=null; foreach($_POST, as $value) { if(!empty($value)continue; Parse error. ;-P There should be no comma there. See http://id2.php.net/manual/en/control-structures.foreach.php this . -- View this message in context: http://www.nabble.com/if-elseif-elseif-elseif-tp22341403p22346913.html Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
$obligatoryFieldNotPresent=null; foreach($_POST, as $value) { if(!empty($value)continue; Parse error. ;-P There should be no comma there. That fixes one, what about the rest? ;) -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
Al wrote: PJ wrote: PJ wrote: Daniel Brown wrote: On Wed, Mar 4, 2009 at 17:51, PJ af.gour...@videotron.ca wrote: elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } Are you certain you only wanted a single equal operator in the last elseif() condition? Further, are you sure it should even be an elseif() and not a straight else? That's where the problem lies... the algorhythm is if any one of a series is empty, then it's an error, but if they are all ls then we go on... So the last one should show up as 0... I tried else $obligatoryFieldNotPresent = 0; but that doesn't want to work. I tried echo $obligatoryFieldNotPresent; just get a blank page... I can't figure out how to determine if anything is in the String... perhaps I should be checking for null elseif ($obligatoryFiledNotPresent == ) { $obligatoryFieldNotPresent = 0; } I tried that too, but same result... finally found the problem... wrong names for string and this is what now verifies correctly if (strlen($_POST[titleIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[first_nameIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[publisherIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[copyrightIN]) == ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[ISBNIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[languageIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (!empty($_POST['categoriesIN'])) { $obligatoryFieldNotPresent = 0; But now I have to figure out the workflow and see why it is not going the right way... wonder where I got some of this stuff... $obligatoryFieldNotPresent=null; foreach($_POST, as $value) { if(!empty($value)continue; $obligatoryFieldNotPresent=true; } Sorry, I was in too big a hurry... $obligatoryFieldNotPresent=null; foreach($_POST as $value) { if(!empty($value))continue; $obligatoryFieldNotPresent=true; } And if you want to know which field(s) failed: $obligatoryFieldNotPresent=array(); foreach($_POST as $key=$value) { if(!empty($value))continue; $obligatoryFieldNotPresent[]=$key; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] if elseif elseif elseif....
This is probably a mysql question, but their list is rather dull - I think they don't appreciate my humor. Beside this list is fun ... and informative. Anyway, I can't figure this out. I am trying to verify inputs on a form and even if I have all the required fields right, I still get the error that there are empty obligatory fields. This is what I am using... but I'm open to anything that works or is more efficient: if (strlen($_POST[titleIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[first_nameIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[publisherIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[copyrightIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[ISBNIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[languageIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[categoriesIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } I've tried closing with else ($obligatoryFiieldNot Present = 0); end; but that give me a blank page - haven't figure out how to check for parsing or scripting errors Thanks for any suggestions. -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
On Wed, Mar 4, 2009 at 17:51, PJ af.gour...@videotron.ca wrote: elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } Are you certain you only wanted a single equal operator in the last elseif() condition? Further, are you sure it should even be an elseif() and not a straight else? -- /Daniel P. Brown daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ 50% Off All Shared Hosting Plans at PilotPig: Use Coupon DOW1 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
Daniel Brown wrote: On Wed, Mar 4, 2009 at 17:51, PJ af.gour...@videotron.ca wrote: elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } Are you certain you only wanted a single equal operator in the last elseif() condition? Further, are you sure it should even be an elseif() and not a straight else? That's where the problem lies... the algorhythm is if any one of a series is empty, then it's an error, but if they are all ls then we go on... So the last one should show up as 0... I tried else $obligatoryFieldNotPresent = 0; but that doesn't want to work. I tried echo $obligatoryFieldNotPresent; just get a blank page... I can't figure out how to determine if anything is in the String... perhaps I should be checking for null elseif ($obligatoryFiledNotPresent == ) { $obligatoryFieldNotPresent = 0; } I tried that too, but same result... -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
PJ wrote: Daniel Brown wrote: On Wed, Mar 4, 2009 at 17:51, PJ af.gour...@videotron.ca wrote: elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } Are you certain you only wanted a single equal operator in the last elseif() condition? Further, are you sure it should even be an elseif() and not a straight else? That's where the problem lies... the algorhythm is if any one of a series is empty, then it's an error, but if they are all ls then we go on... So the last one should show up as 0... Using a single = means an assignment. Assigning a variable should never fail. You probably want == to do a comparison. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
Chris wrote: PJ wrote: Daniel Brown wrote: On Wed, Mar 4, 2009 at 17:51, PJ af.gour...@videotron.ca wrote: elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } Are you certain you only wanted a single equal operator in the last elseif() condition? Further, are you sure it should even be an elseif() and not a straight else? That's where the problem lies... the algorhythm is if any one of a series is empty, then it's an error, but if they are all ls then we go on... So the last one should show up as 0... Using a single = means an assignment. Assigning a variable should never fail. You probably want == to do a comparison. Yes and yes, however the it's the result of the assignment that is used: if($var = false) { echo Succeeded; } else { echo FAIL; } -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
PJ wrote: Daniel Brown wrote: On Wed, Mar 4, 2009 at 17:51, PJ af.gour...@videotron.ca wrote: elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } Are you certain you only wanted a single equal operator in the last elseif() condition? Further, are you sure it should even be an elseif() and not a straight else? That's where the problem lies... the algorhythm is if any one of a series is empty, then it's an error, but if they are all ls then we go on... So the last one should show up as 0... I tried else $obligatoryFieldNotPresent = 0; but that doesn't want to work. I tried echo $obligatoryFieldNotPresent; just get a blank page... I can't figure out how to determine if anything is in the String... perhaps I should be checking for null elseif ($obligatoryFiledNotPresent == ) { $obligatoryFieldNotPresent = 0; } I tried that too, but same result... finally found the problem... wrong names for string and this is what now verifies correctly if (strlen($_POST[titleIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[first_nameIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[publisherIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[copyrightIN]) == ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[ISBNIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[languageIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (!empty($_POST['categoriesIN'])) { $obligatoryFieldNotPresent = 0; But now I have to figure out the workflow and see why it is not going the right way... wonder where I got some of this stuff... -- unheralded genius: A clean desk is the sign of a dull mind. - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
Shawn McKenzie wrote: Chris wrote: PJ wrote: Daniel Brown wrote: On Wed, Mar 4, 2009 at 17:51, PJ af.gour...@videotron.ca wrote: elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } Are you certain you only wanted a single equal operator in the last elseif() condition? Further, are you sure it should even be an elseif() and not a straight else? That's where the problem lies... the algorhythm is if any one of a series is empty, then it's an error, but if they are all ls then we go on... So the last one should show up as 0... Using a single = means an assignment. Assigning a variable should never fail. You probably want == to do a comparison. Yes and yes, however the it's the result of the assignment that is used: if($var = false) { echo Succeeded; } else { echo FAIL; } Yeh that's what I was trying to say, just did it poorly ;) -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
PJ wrote: PJ wrote: Daniel Brown wrote: On Wed, Mar 4, 2009 at 17:51, PJ af.gour...@videotron.ca wrote: elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } Are you certain you only wanted a single equal operator in the last elseif() condition? Further, are you sure it should even be an elseif() and not a straight else? That's where the problem lies... the algorhythm is if any one of a series is empty, then it's an error, but if they are all ls then we go on... So the last one should show up as 0... I tried else $obligatoryFieldNotPresent = 0; but that doesn't want to work. I tried echo $obligatoryFieldNotPresent; just get a blank page... I can't figure out how to determine if anything is in the String... perhaps I should be checking for null elseif ($obligatoryFiledNotPresent == ) { $obligatoryFieldNotPresent = 0; } I tried that too, but same result... finally found the problem... wrong names for string and this is what now verifies correctly if (strlen($_POST[titleIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[first_nameIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[publisherIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[copyrightIN]) == ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[ISBNIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[languageIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (!empty($_POST['categoriesIN'])) { $obligatoryFieldNotPresent = 0; But now I have to figure out the workflow and see why it is not going the right way... wonder where I got some of this stuff... Glad to hear it, however just some critique of your coding style while you're still learning. You should leave no doubt as to what variables are for. Be descriptive, like: $theObligatoryFieldFromTheFormTheOneThatMustBePresentAndContainAvalueMayInFactBePresentOrYouWouldReceiveAnE_NOTICEHoweverTheFieldContainsNoValue = 1 Also look into isset() and empty(). -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
PJ wrote: PJ wrote: Daniel Brown wrote: On Wed, Mar 4, 2009 at 17:51, PJ af.gour...@videotron.ca wrote: elseif ($obligatoryFieldNotPresent = 1) { $obligatoryFieldNotPresent = 0; } Are you certain you only wanted a single equal operator in the last elseif() condition? Further, are you sure it should even be an elseif() and not a straight else? That's where the problem lies... the algorhythm is if any one of a series is empty, then it's an error, but if they are all ls then we go on... So the last one should show up as 0... I tried else $obligatoryFieldNotPresent = 0; but that doesn't want to work. I tried echo $obligatoryFieldNotPresent; just get a blank page... I can't figure out how to determine if anything is in the String... perhaps I should be checking for null elseif ($obligatoryFiledNotPresent == ) { $obligatoryFieldNotPresent = 0; } I tried that too, but same result... finally found the problem... wrong names for string and this is what now verifies correctly if (strlen($_POST[titleIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[first_nameIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[publisherIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[copyrightIN]) == ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[ISBNIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[languageIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (!empty($_POST['categoriesIN'])) { $obligatoryFieldNotPresent = 0; But now I have to figure out the workflow and see why it is not going the right way... wonder where I got some of this stuff... $obligatoryFieldNotPresent=null; foreach($_POST, as $value) { if(!empty($value)continue; $obligatoryFieldNotPresent=true; } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
finally found the problem... wrong names for string and this is what now verifies correctly if (strlen($_POST[titleIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[first_nameIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[publisherIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[copyrightIN]) == ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[ISBNIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (strlen($_POST[languageIN]) == 0 ) { $obligatoryFieldNotPresent = 1; } elseif (!empty($_POST['categoriesIN'])) { $obligatoryFieldNotPresent = 0; I'd suggest something slightly different. $errors = array(); if (empty($_POST['title'])) { $errors[] = 'title'; } if (empty($_POST['first_nameIN'])) { $errors[] = 'first_nameIN'; } ... # If we found any errors, show a message! if (!empty($errors)) { echo You forgot to fill in these fields:br/; echo implode(',', $errors) . br/; } So your user knows what they missed. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] if elseif elseif elseif....
On Wed, Mar 4, 2009 at 20:10, Al n...@ridersite.org wrote: $obligatoryFieldNotPresent=null; foreach($_POST, as $value) { if(!empty($value)continue; Parse error. ;-P -- /Daniel P. Brown daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ 50% Off All Shared Hosting Plans at PilotPig: Use Coupon DOW1 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php