Jump to content

How to handle text fields not filled out?


RynMan

Recommended Posts

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? 

Link to comment
Share on other sites

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'] != '')

Link to comment
Share on other sites

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'] != '')

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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! ;)

Link to comment
Share on other sites

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.
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.