if (isset($_POST['catid']) && ($_POST['catid'] == 1) || (isset($_POST['catid']) && ($_POST['catid'] == 4))){ } PHP: Is there an easier way of doing this other than turning off error reporting for this as some stuff gets really repetitive and long winded: if (isset($_POST['fname']) && ($_POST['fname']) && (isset($_POST['fname']) && ($_POST['sname']) && (isset($_POST['email']) && ($_POST['email']) && (isset($_POST['pass']) && ($_POST['pass']) && (isset($_POST['postcode']) && ($_POST['postcode']) && (isset($_POST['agerange']) && ($_POST['agerange']))))))) { PHP: I thought maybe I could put $_POST into a class and check them all at once but that didn't work. Any ideas?
firstly don't trust any users input. always validate and clean. so here would be my way of doing this. if (isset($_POST['cat'])) { $catid = (int)$_POST['cat']; } else { $catid = 0; } if (in_array($catid,array(1,4))) { } PHP: or if you end up adding more catid's if (isset($_POST['cat'])) { $catid = (int)$_POST['cat']; } else { $catid = 0; } switch ($catid) { case 1: case 4: // simply add more id's // case 6: // case 19: // code you want to run goes here break; default : // if you want to run any code if cat id is not 1 or 4 } PHP: keep the code clean simple and easy readable.
you could also do this if you wanted to do it rally short if ( isset($_POST['cat']) && in_array( (int)$_POST['cat'], array(1,4) ) ) { } PHP:
How would you handle a long one like this though? if (isset($_POST['fname']) && ($_POST['fname']) && (isset($_POST['fname']) && ($_POST['sname']) && (isset($_POST['email']) && ($_POST['email']) && (isset($_POST['pass']) && ($_POST['pass']) && (isset($_POST['postcode']) && ($_POST['postcode']) && (isset($_POST['agerange']) && ($_POST['agerange']))))))) { PHP:
I recommed to create function for this, like this (off course you can edit by self): function postValue($NAME) { if(isset($_POST[$NAME]) && !empty($_POST[$NAME])) { return mysql_real_escape_string(htmlspecialchars($_POST[$NAME])); } } function getValue($NAME) { if(isset($_GET[$NAME]) && !empty($_GET[$NAME])) { return mysql_real_escape_string(htmlspecialchars(urldecode($_GET[$NAME]))); } } PHP: And use it: if(postValue("post_name")) { #YES } else { #NOT } PHP:
Note that isset() can take multiple arguments. if (isset($_POST['foo'], $_POST['bar'], ...)) { // ... all set } PHP:
You should use !empty() instead of isset(). empty() checks that it is set and not NULL. isset() allows it to be empty.
A function for checking them? Are you joking? You create functions to reuse code over and over. Creating a function is overkill. Now OP asks for the "easiest" way. "easiest" way makes for crappy code. Do you want the easiest way or the smartest way? <?php if (!function_exists('sqlclean')) { function sqlclean($data) { if (filter_var($data, FILTER_VALIDATE_INT) !== FALSE) { return $data; } else { $data = urldecode($data); $data = filter_var($data, FILTER_SANITIZE_STRING); return mysql_real_escape_string($data); } } } if (!empty($_POST['catid'])){ if ($_POST['catid'] == 1 || $_POST['catid'] == 4) { echo 'cat id is either 1 or 4';exit; } else { echo 'cat id is neither 1 nor 4.';exit; } } else { echo 'no cat id sent';exit; } $required = array('fname','sname','email','pass','postcode','agerange'); foreach ($required AS $req) { if (empty($_POST[$req])) { switch($req) { case 'fname': echo 'You left the first name blank.'; break; case 'sname': echo 'You left the last name blank.'; break; case 'email': echo 'You left the email blank.'; break; case 'password': echo 'You left the password blank.'; break; case 'postcode': echo 'You left the postal code blank.'; break; case 'agerange': echo 'You left the age range blank.'; break; } exit; } else { $$req = sqlclean($_POST[$req]); } } ?> PHP: