i have done something like this:
<form action="validate.php" method="get">
Id :<input type="text" name="pID"/><br/><br/>
Name :<input type="text" name="pName"/><br/><br/>
Description :<input type="text" name="pDesc"/><br/><br/>
Price :<input type="text" name="pPrice"/><br/><br/>
<input type="submit" name="pSub"/>
</form>
my validate.php contains :
<?php
if (!empty($_GET['pID']) || !empty($_GET['pName']) || !empty($_GET['pDesc']) || !empty($_GET['pPrice'])){
if(is_numeric($_GET['pID']) || is_numeric($_GET['pPrice']))
{
echo "</br>Your ID :".$_GET["pID"]."</br>";
echo "Name is :".$_GET["pName"]."</br>";
echo "Description :".$_GET["pDesc"]."</br>";
echo "and Price :".$_GET["pPrice"]."</br>";
}
else{echo "Pls See That ID and Price are Numerical";}
}else{
echo "Fill up All The Values";
}
?>
is not working properly ,
1st if conditions doesn’t work properly
ie. if i left blank “Name” input field message should have come saying
“Fill up All The Values “…… instead its showing the list of inputs
is there any other way to validate form (PHP)
You are using the wrong operator:
||means “logical OR”; what you seem to be looking for is&&, that is “logical AND”.The code does exactly what you told it to do (see the documentation); the fact that you intended something else is not relevant to the computer:
means “if pID is not empty OR pName is not empty OR …”; as soon as one or more of the fields are not empty, the condition evaluates to true.
What you can do to get what you meant:
&&)if (!(empty($_GET['pID']) || empty($_GET['pID'] ...))– note that the whole expression is negated in parentheses(read on De Morgan’s laws to see why these two solutions are equivalent)