Forum Moderators: coopster

Message Too Old, No Replies

Finding the length of a field in a form

Specifying the length of a field in a form

         

Adam5000

8:41 pm on Sep 28, 2010 (gmt 0)

10+ Year Member



I'm trying to do some error checking with PHP. I've got a javascript form that works good and I'm trying to create a PHP form that does the same thing.

One thing I want to check for is empty fields with an if else statement.
The javascript version looks like this.


function check_field()
{
var usr_name = document.getElementById('u_name')

if (usr_name.value.length == 0)
{
alert ("Field is empty")
return = false
}

else
{
alert ("This field has value")
}
}

<form action="test.htm" method="post" onsubmit "return= check_field()">
username: <input type="text">
<input type="submit" value="Submit">
</form>

And this is the PHP code I've tried.

function check_field()

$_POST["user_name"]

if $_POST.length["user_name"] == 0

echo "This field is empty";

else

echo "This field has value";

<form action="test.htm" method="post" onsubmit "return= check_field()">
username: <input type="text">
<input type="submit" value="Submit">
</form>

How do I specify the length value to say "If the length of the field is 0 then display the message 'Field is empty'"

Help!

Demaestro

10:20 pm on Sep 28, 2010 (gmt 0)

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



You are close you just need to wrap the condition in brackets () and the outputs in curlies, and how you are checking the lenth needs to be changed. You can use strlen() which returns the length of a string.

if (strlen($_POST["user_name"]) == 0){
echo "This field is empty";
}
else
{
echo "This field has value";
}

Demaestro

10:28 pm on Sep 28, 2010 (gmt 0)

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



.... also if you want you can "trim" the string first to remove any blank spaces.

If someone types in a single blank space then your script will echo "This field has value" because it has a length of 1... a space.

You can call trim which removes any leading or trailing whitespace.... so if you trim it then test it's length you will trap blank space entries as well.

(strlen(trim($_POST["user_name"])) == 0)

Adam5000

4:23 pm on Sep 29, 2010 (gmt 0)

10+ Year Member



That's great Demaestro. I've put the code together but I still have a syntax error somewhere that I can't seem to find. Below is the code I'm using.

Help!

<html>
<head>
<title>Length test</title>
</head>

<body>

<?php

function check_fields()
{
$_POST["user_name"]

if (strlen(trim($_POST["user_name"])) == 0)

{
echo "This field is empty";
}

else
{
echo "This field has value";
}
}
?>


<form action="length_of_a_form_field.htm" method="post" onsubmit="return check_fields()">

username: <input type="text" name="user_name">
<input type="submit" value="Submit">

</form>

</body>
</html>

Matthew1980

4:40 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<?php

function check_fields(){
$_POST['user_name'];//missing the quote, and this isn't needed here so you can delete this line

if (strlen(trim($_POST["user_name"])) == 0){
echo "This field is empty";
}
else{
echo "This field has value";
}
}
?>


But I still think that calling this PHP function from javascript isn't going to work, It doesn't function like that JS is client side, PHP is server side, it's a one way street, php can manipulate js data quite easily, but JS cannot do that to php as it is compiled server side!

Hope that makes sense.

Cheers,
MRb

Adam5000

8:03 pm on Sep 29, 2010 (gmt 0)

10+ Year Member



You"re likely right Matthew, calling the function from javascript probably isn't going to work. What's a good way to call it from PHP?

Demaestro

8:29 pm on Sep 29, 2010 (gmt 0)

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



Typically when you are doing server side validation you post the form to the PHP function.

It checks for inputs and if the form validates then it continues on... if it fails then redirect to the form and redraw the form with the values prefilled out along with some text about why validation failed.

Pseudo code goes like this:

$form_validates = True;
$message_array = [];

if (strlen(trim($_POST["user_name"])) == 0)
{$form_validates = False;
$message_array[] = 'Username is empty';}


if (strlen(trim($_POST["password"])) == 0)
{$form_validates = False;
$message_array[] = 'Password is empty';}

if (form_validates == True) {
pass
}
else: {
redraw form with values prefilled
}

rocknbil

9:43 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Follow their lead, you will have to validate server side, but if you want JS, you have your returns wrong, it's not an = which sets a variable, it returns using the return keyword. Basically if the form passes validation, return true. Like

<form action="can't post to an .htm unless you are parsing .htm as PHP" method="post" onsubmit "return check_field()">


<script type="text/javascript">
function check_field() {
var usr_name = document.getElementById('u_name');
if (usr_name.value == '') {
alert ("Field is empty");
return false;
}
else {
return true;
}
}
</script>

Adam5000

10:00 pm on Sep 29, 2010 (gmt 0)

10+ Year Member



Demastero: That looks like a good way to do it and it has been a day of hard work. I'm going to pick it up again in the morning. You're definitely "De Maestro"

Adam5000

10:07 pm on Sep 29, 2010 (gmt 0)

10+ Year Member



Pardon my spelling "De Maestro"

Thanks rocknbil. I think you're right. The best idea is to validate on the server side with PHP.

It's always cool when you're here rocknbil (Smile).