0

I need to validate an empty field with php and javascript, but both of the methods fail.

<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<input type="text" name="pickupaddress"  value="<?
 if($pickupaddress == ''){ 
echo "";} 
else{echo htmlentities($pickupaddress);}?> " id="pickupaddress"/>
<input type ="submit" name="submit" value"Reserve"/> 
</form>

//////// Php validation DOES NOT WORK////////

$pickupaddress ='';
$err ='';

$pickupaddress = $_POST['pickupaddress'];

if($pickupaddress == ''){   //if empty field, I also tried == ""
$err.="Please provide pick up address.";
}

///// Javascript validation does not work.


if(form.pickupaddress ==""){
alert("empty address!");
}

//when I click submit nothing happens. //I am thinking the problem is with

htmlentities($pickupaddress);

//Thanks for your help.

3
  • var_dump($_POST['pickupaddress']); returns what ? Commented Aug 13, 2012 at 22:25
  • you have a white space in the value of your pickupaddress field value. .. so since ' ' != '' your validators never work. Commented Aug 13, 2012 at 22:40
  • Also noticed one little typo in value"Reserve" Commented Aug 13, 2012 at 23:02

5 Answers 5

1

Here is hopefully simpler answer:

$pickupaddress = trim($_POST['pickupaddress']); //trims the string
    if (empty($pickupaddress)){   //if empty field
    $err.="Please provide pick up address.";
}
Sign up to request clarification or add additional context in comments.

Comments

1

On the php side you can try trimming the value and then using empty() on the next line, though that will also invalidate 0, false, null, and other such values. Or you can try using isset.

For the javascript side you can try this function:

function IsEmpty(aTextField) {
    if ((aTextField.value.length==0) ||
       (aTextField.value==null)) {
        return true;
    }
    else { return false; }
}

found here: http://www.codetoad.com/javascript/isempty.asp

Comments

1
$cid = $_POST['category'];

if (!empty($_POST['category']))
{
   echo "<script>alert('empty field');</script>";
}

1 Comment

Hi and welcome to SO! Although the code may speak for itself providing some details would help improve the quality of your answer!
0

Where are you defining pickupaddress? Is it before the form or after? If the variable isn't defined, and depending on your server configuration, the value field of the input could be

Notice: undefined variable pickupaddress

Thus making the value != ''.

View your page source to ensure that the value is indeed empty.

Comments

0

there was a spave " " in your string: echo htmlentities($pickupaddress);}?> " maybe that was the reason, because it was not an empty string but it was a space?

<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">

<input type="text" name="pickupaddress"  value="<?
if($pickupaddress != '') {
    echo htmlentities($pickupaddress);
}?>" id="pickupaddress"/>
<input type ="submit" name="submit" value"Reserve"/> 

</form>

and i guess you might want to have checked if the post value is set:

if(isset($_POST['pickupaddress'])) {
    $pickupaddress = $_POST['pickupaddress'];
}

the php way works for me like that ;) (the message is displayed if i dont write anything)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.