RynMan Posted July 11, 2009 Share Posted July 11, 2009 Hi guys Just wondering how you guys handle input fields that aren't filled out? Say I have a form with a text input to search for someone's name, and a submit button. The following row returns the matching names. If say, the input for the person's name to search for wasn't filled out, how would you proceed? Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/ Share on other sites More sharing options...
BillyBoB Posted July 11, 2009 Share Posted July 11, 2009 You can always make exceptions. Like: if(isset($_POST['random_field'])) { //do stuff its there }else{ //tell the user its not filled in. } Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/#findComment-873459 Share on other sites More sharing options...
thebadbad Posted July 11, 2009 Share Posted July 11, 2009 You can always make exceptions. Like: if(isset($_POST['random_field'])) { //do stuff its there }else{ //tell the user its not filled in. } When the field random_field exists in the form, $_POST['random_field'] will always be set, even if the field was empty when submitted (at least in Firefox 3.5 and IE8). empty() will work, but will also return true if the user enters a zero. You could use if ($_POST['random_field'] != '') Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/#findComment-873465 Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 if ($_POST['random_field'] != '') This will work in FF 3.5 & IE8 as you noted but will throw: Notice: undefined index: 'random_field' in all other browsers. if (isset($_POST['random_field'][2])) { // makes sure random_field contains atleast 3 characters (thus not empty) whilst not throwing any notices //set } else { //not set } but will also return true if the user enters a zero And how is this then a solution? if ($_POST['random_field'] != '') Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/#findComment-873472 Share on other sites More sharing options...
thebadbad Posted July 11, 2009 Share Posted July 11, 2009 but will also return true if the user enters a zero And how is this then a solution? if ($_POST['random_field'] != '') Well, that simply checks if the field was empty when submitted, i.e. what the OP wanted. If a single zero isn't allowed anyway, empty() would be ideal to use for checking (to get rid of any notices). Else, you can use something like <?php function empty_post($key) { if (isset($_POST[$key])) { if ($_POST[$key] !== '') { return false; } } return true; } //then if (empty_post('random_field')) { //$_POST['random_field'] is an empty string, or isn't set } else { //$_POST['random_field'] contains something } ?> Your solution is useless if e.g. 1 or 2 characters are valid. And I'm finding it hard to believe that all other browsers would handle the form data differently. Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/#findComment-873502 Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 Your solution is useless if e.g. 1 or 2 characters are valid. 1) My solution is anything but useless. 2) You would ofcourse modify it to the minimum of characters the input field would need. And I'm finding it hard to believe that all other browsers would handle the form data differently. Ok you are correct on this one, my apologies. However: but will also return true if the user enters a zero You suggested: if ($_POST['random_field'] != '') returns true if random_field contains a 0 as empty() does, yes My suggestion: if (isset($_POST['random_field'][2])) only returns true if random_field atleast contains 3 characters, ok granted they can still just type 000 but an added ctype_alpha($_POST['random_field']) should solve that. My suggestion to solve some of the shortcomings isset() and empty() have. Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/#findComment-873569 Share on other sites More sharing options...
thebadbad Posted July 11, 2009 Share Posted July 11, 2009 Sorry for sounding a bit harsh back there. Just found that if (isset($_POST['random_field'][0])) works brilliant. If the string is an empty string, it returns false (the problem with your initial isset($_POST['random_field']) was, that it would return true in that case). So I take my words back, and thank you for a smart solution! Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/#findComment-873592 Share on other sites More sharing options...
ignace Posted July 11, 2009 Share Posted July 11, 2009 Sorry for sounding a bit harsh back there. Just found that if (isset($_POST['random_field'][0])) the problem with your initial isset($_POST['random_field']) was, that it would return true in that case Was not posted by me, but by BillyBob You can always make exceptions. Like: if(isset($_POST['random_field'])) { //do stuff its there }else{ //tell the user its not filled in. } Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/#findComment-873644 Share on other sites More sharing options...
thebadbad Posted July 11, 2009 Share Posted July 11, 2009 Oh, I guess your avatars threw me off Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/#findComment-873657 Share on other sites More sharing options...
npsari Posted July 11, 2009 Share Posted July 11, 2009 You can use Javascript if you prefer, however, PHP is better for me, because I can do more with it Or you can use both Quote Link to comment https://forums.phpfreaks.com/topic/165598-how-to-handle-text-fields-not-filled-out/#findComment-873688 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.