dave25 Posted May 4, 2015 Share Posted May 4, 2015 (edited) How to filter input in array and declare all variables to prevent undefined index. I know how to use filter_input_array(INPUT_POST, ...) and filter_input_array(INPUT_GET, ...) separately, but i donk know how to use them together. Current code for testing. <?php $errors = array(); /* FILTER AND DECLARE VARIABLES */ $input['a'] = isset($_POST['a']) ? filter_input(INPUT_POST, 'a', FILTER_SANITIZE_STRING) : NULL; $input['b'] = isset($_POST['b']) ? filter_input(INPUT_POST, 'b', FILTER_SANITIZE_NUMBER_INT) : NULL; $input['c'] = isset($_GET['c']) ? filter_input(INPUT_GET, 'c', FILTER_SANITIZE_STRING) : NULL; $input['d'] = isset($_GET['d']) ? filter_input(INPUT_GET, 'd', FILTER_SANITIZE_NUMBER_INT) : NULL; $input['e'] = isset($_POST['e']) ? filter_input(INPUT_POST, 'e', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY) : NULL; /* $input = array( 'a' => NULL, 'b' => NULL, 'e' => NULL ); */ if(filter_has_var(INPUT_POST, 'submit')) { /* $defs = array( 'a' => FILTER_SANITIZE_STRING, 'b' => FILTER_SANITIZE_NUMBER_INT, 'e' => array('filter' => FILTER_SANITIZE_NUMBER_INT, 'flags' => FILTER_REQUIRE_ARRAY ) ); */ if(empty($input['a'])) { $errors[] = 'Error a.'; } if(empty($input['b'])) { $errors[] = 'Error b.'; } } if(filter_has_var(INPUT_POST, 'submit') and empty($errors)) { /* QUERYES */ print 'SUCCESS'; } if(count($errors)) { foreach($errors as $error) { print $error; } } print " <form method='post'> A: <input type='text' name='a' value='".$input['a']."'> POST STRING <br> B: <input type='text' name='b' value='".$input['b']."'> POST INT <br> C: <input type='text' readonly value='".$input['c']."'> GET STRING <br> D: <input type='text' readonly value='".$input['d']."'> GET INT <br> <input type='submit' name='submit' value='SUBMIT'> </form>"; ?> Edited May 4, 2015 by dave25 Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted May 5, 2015 Share Posted May 5, 2015 You could first detect what method is actually being used $_SERVER['REQUEST_METHOD'] It's even possible to use $_REQUEST instead Quote Link to comment Share on other sites More sharing options...
dave25 Posted May 7, 2015 Author Share Posted May 7, 2015 Any example Quote Link to comment Share on other sites More sharing options...
Muddy_Funster Posted May 7, 2015 Share Posted May 7, 2015 Any example Seriously? if($_SERVER['REQUEST_METHOD'] == "POSTArray"){ //Process POST var's } elseif($_SERVER['REQUEST_METHOD'] == "GETArray"){ //Process GET var's } Quote Link to comment Share on other sites More sharing options...
dave25 Posted May 7, 2015 Author Share Posted May 7, 2015 (edited) I have tried with this way and its working and also dosent display any undefined indexes. $input_POST = array( 'a' => NULL, 'b' => NULL, 'e' => NULL ); $input_GET = array( 'c' => NULL, 'd' => NULL ); $input = array_merge($input_POST, $input_GET); if(!empty($_GET)){ $defs_GET = array( 'c' => FILTER_SANITIZE_STRING, 'd' => FILTER_SANITIZE_NUMBER_INT ); $input_GET = filter_input_array(INPUT_GET, $defs_GET); } if(!empty($_POST)){ $defs_POST = array( 'a' => FILTER_SANITIZE_STRING, 'b' => FILTER_SANITIZE_NUMBER_INT, 'e' => array('filter' => FILTER_SANITIZE_NUMBER_INT, 'flags' => FILTER_REQUIRE_ARRAY) ); $input_POST = filter_input_array(INPUT_POST, $defs_POST); } $input = array_merge($input_POST, $input_GET); if(filter_has_var(INPUT_POST, 'submit')){ if(empty($input['a'])) { $errors[] = 'Error a.'; .......................................................... Edited May 7, 2015 by dave25 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted May 7, 2015 Share Posted May 7, 2015 You could try something like this: //IF POST OR GET METHOD WAS USED if(in_array($_SERVER['REQUEST_METHOD'], array('POST', 'GET'))) { //DETERMINE WHICH FILTER TO USE $howToFilter = ($_SERVER['REQUEST_METHOD']=='POST') ? INPUT_POST : INPUT_GET; //GET INPUT $input['a'] = isset($_REQUEST['a']) ? filter_input($howToFilter, 'a', FILTER_SANITIZE_STRING) : NULL; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.