tomd79 Posted June 29, 2008 Share Posted June 29, 2008 Hi all I'm guessing this is another real simple question!! I have a HTML form with a number of check boxes that creates an array for my php. The form has one text field for a user to add a unique entry not available from the existing check boxes (see html code) <input type="checkbox" name="players[]" value="somevalue" /> some value<br /> <input type="checkbox" name="players[]" value="somevalue1" /> some value 1<br /> <input type="text" name="players[]" /> Please input players name<br /> The problem I'm having is the text input is being parsed in the array even if its empty (i.e User doen't want to add another unique value)... What I want to happen is the text input is parsed if a value is entered but is simply ignored if the text input is empty. I do NOT want it to report an error is the text input is empty..... I've been browsing the web and can see loads of documention on form validation, but everything seems to be about validating inputs or throwing errors. I can't find anything on simply ignoring empty fields and making sure they don't get submitted... My guess is that the $post has to send the empty text input, so the PHP will have to have a check to look for empty inputs and disregard them? Quote Link to comment https://forums.phpfreaks.com/topic/112436-stopping-empty-inputs-being-parsed-to-php-array/ Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 Any input that is in a form will be submitted with the form regardless of it's content. This doesn't matter though because you can choose what to do with it when you process it. For example, if you don't want an error to be displayed then simply don't throw an error because it's empty! This isn't an automatic thing, you must actually do it yourself so just don't. Quote Link to comment https://forums.phpfreaks.com/topic/112436-stopping-empty-inputs-being-parsed-to-php-array/#findComment-577225 Share on other sites More sharing options...
tomd79 Posted June 29, 2008 Author Share Posted June 29, 2008 this isn't working but am I along the right lines? foreach ($players as $key => $value) { if (is_null($value) || $value=="") { unset($players [$key]); } } Quote Link to comment https://forums.phpfreaks.com/topic/112436-stopping-empty-inputs-being-parsed-to-php-array/#findComment-577260 Share on other sites More sharing options...
br0ken Posted June 29, 2008 Share Posted June 29, 2008 Yes kind of, although I'm not entirely sure if you can unset a single element of an array. Couldn't you just set the value in empty ones to 0 and have this signify that the element is empty? Quote Link to comment https://forums.phpfreaks.com/topic/112436-stopping-empty-inputs-being-parsed-to-php-array/#findComment-577262 Share on other sites More sharing options...
tomd79 Posted June 29, 2008 Author Share Posted June 29, 2008 I have no idea i'm a total noob and completely lost Quote Link to comment https://forums.phpfreaks.com/topic/112436-stopping-empty-inputs-being-parsed-to-php-array/#findComment-577456 Share on other sites More sharing options...
tomd79 Posted June 29, 2008 Author Share Posted June 29, 2008 would anyone help with this as I'm pulling my hair out and getting nowhere!! This is a 'simple' team picker for our works 5 aside.. its working, but I have tried to add a field to the form where a guest players name can be inserted.. The problem is if no guest is playing the empty field is still sent, and counted in the PHP.. The html form: <form action="sort.php" method="POST"> <input type="checkbox" name="players[]" value="name" /> name <br /> <input type="checkbox" name="players[]" value="name1" /> name 1<br /> <input type="checkbox" name="players[]" value="name2" /> name 2<br /> <input type="checkbox" name="players[]" value="name3" /> name 3<br /> <input type="text" name="players[]" /> Enter guests name<br /> <br /> <input type="submit" value="Pick the teams!"> </form><br /> The php: <?php shuffle( $_POST['players'] ); $player_count = count( $_POST['players'] ); $team_size = ceil( $player_count / 2 ); list( $team1, $team2 ) = array_chunk( $_POST['players'], $team_size ); echo "<h2>Team 1</h2>"; echo "<b>Player count:</b> " . count( $team1 ) . "<br />"; echo "<b>Player list:</b>"; echo "<ul>"; foreach ( $team1 as $player ) { echo "<li>$player</li>"; } echo "</ul>"; echo "<h2>Team 2</h2>"; echo "<b>Player count:</b> " . count( $team2 ) . "<br />"; echo "<b>Player list:</b>"; echo "<ul>"; foreach ( $team2 as $player ) { echo "<li>$player</li>"; } echo "</ul>"; ?> I have tried alsorts but cant get the empty elements removed before the shuffle.. help would be greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/112436-stopping-empty-inputs-being-parsed-to-php-array/#findComment-577482 Share on other sites More sharing options...
tomd79 Posted June 30, 2008 Author Share Posted June 30, 2008 can someone purhaps help by pointing me to the most appropriate command to search $POST (['players']) for empty lines in the array? I'm really stuck and need a pointer.. Quote Link to comment https://forums.phpfreaks.com/topic/112436-stopping-empty-inputs-being-parsed-to-php-array/#findComment-577964 Share on other sites More sharing options...
MasterACE14 Posted June 30, 2008 Share Posted June 30, 2008 check out in_array() function http://au2.php.net/manual/en/function.in-array.php Quote Link to comment https://forums.phpfreaks.com/topic/112436-stopping-empty-inputs-being-parsed-to-php-array/#findComment-577970 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.