Forum Moderators: coopster

Message Too Old, No Replies

Blanks in submitted form

Blanks in form input

         

mvaz

1:06 pm on Jul 21, 2008 (gmt 0)

10+ Year Member



Hello, I have a form with a few inputs and this input is emailed to my email address after ensuring that the required fields are populated.

However, if a user, instead of typing their 'Name', only hit the space bar, creating a text with no 'real' characters, the form gets emailed with blanks in the 'Name' field.

Is there any way that this can be verified before the input is emailed or sent to the database?

brotherhood of LAN

2:25 pm on Jul 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You might want to use some javascript validation, and definitely some PHP validation

if(!trim($var))
echo 'This variable is just whitespace';

PHP_Chimp

2:27 pm on Jul 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes.

As this is a very large topic I would suggest that you google for PHP form validation.

Once you have some code then you could always ask for comments on it.

A (very) basic example.


if ($_POST) {
// form has been posted
if (trim($_POST['first_name']) = '');
{
echo 'Fill in the form properly. Go back and do it again.';
}
}
else {
// display page
}

You may or may not want to do the validation of the form on the same page. Some people like to others dont.

[edited by: PHP_Chimp at 2:27 pm (utc) on July 21, 2008]

dreamcatcher

3:19 pm on Jul 21, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can trim all your post vars in one go using array_map [php.net].

$_POST = array_map('trim',$_POST);

dc

NeilsPHP

7:13 pm on Jul 21, 2008 (gmt 0)

10+ Year Member




You can verify if that part of form has not been filled by using 'empty' logic as follows:
after form is submitted,receive it using this way:
e.g.lets say there is a form to enter first name

$FirstName = empty($_POST['FirstName']) ? die ("ERROR: Enter Your First Name ") : mysql_real_escape_string($_POST['FirstName']);

this will give error to user that he needs to enter his first name or form won't be submitted.
hope this helps