How can i check 2 text fields are empty with PHP.
Here is exactly what i want
there are 2 text fields in my form. I dont want the form to be submitted if both fields are empty. But if one of the text fields have a entered value form should get submitted.
I have tired this code but it wont submit if both fields values are entered.
if($_POST['inputOne'] == NULL AND $_POST['inputTwo'] == NULL )
{
die('My Error Msg.');
}
Can anyone tell me how to do this.
Thanks to Fred -ii- (with edits to he's code) i have found an answer
if(empty($_POST['inputOne']) AND empty($_POST['inputTwo']))
{
die('My Error Msg.');
}
If you dont use AND it will not look for both fields empty together instead it will look of each field separately.
if($_POST['inputOne'] == NULL OR $_POST['inputTwo'] == NULL )
"But still this look for both fields to have a value. Adding value to one field will not post the form"
Use the following then. It will check if one or both are empty.
if(empty($_POST['inputOne']) || empty($_POST['inputTwo'])){...}
instead of using == NULL
FYI: || is the same as using OR
Consult: http://php.net/manual/en/language.operators.logical.php
Or as you state in your answer: (using AND)
if(empty($_POST['inputOne']) && empty($_POST['inputTwo'])){...}
where && is the same as AND - just another quick "FYI".
It saves you a keystroke (wink)
There should be an OR instead of the AND.
if($_POST['inputOne'] == NULL OR $_POST['inputTwo'] == NULL )
{
die('My Error Msg.');
}
EDIT: If OR doesn't suffice try:
if(empty($_POST['inputOne']) || empty($_POST['inputTwo']))
{
die('My Error Msg.');
}
Related
I have Ajax posting to a php script. One of the posts is a . In the php script, I check if an option in the dropdown was selected. If not, I fill in a default value. This is then submitted to a database. See my below code for checking if an was selected:
if($_POST['dropdownValue'] == null){}
99.9% of the time, this works. I don't select an option, and the default value is returned and this is stored in my database. But now I'm finding NULL rows in my database that are filled with the value of $_POST['dropdownValue']. Should I be using the function is_null()? Or isset()? I saw another post that said to check it with $_POST['dropdownValue'] == ''. Would that be better?
you could try using the isset for this i think
if(isset($_POST['dropdownValue']) && ($_POST['dropdownValue'] != null)
{
'insert that data into my base'
}
Let's just use isset:
if( isset($_POST['fromPerson']) )
{
$fromPerson = '+from%3A'.$_POST['fromPerson'];
echo $fromPerson;
}
if(isset($_POST["submit"])){
if (isset($_POST["name"]) && isset($_POST["roll"])){ // checking whether both text boxes are filled up
echo "$_POST[name] $_POST[roll]"; // just displays some info
}
else{ //not being executed when both text boxes are empty
header("Location:interface.php");
}
}
the above is a php code where im trying to check whether two text boxes are filled in or else i will navigate to another page. The problem is though if i fill up one text box the && condition doesn't work , it gets into the if part and echoes one value.
There is error (missing ') in:
echo "$_POST[name] $_POST[roll]";
It has to be $_POST['name'] and $_POST['roll']
UPDATE
isset() only is not enough. You should also check if it is not empty, like this:
if (!empty($_POST["name"]) && !empty($_POST["roll"])){
I tried to make a form with a post method, and i want to check if the fields are filled with isset(). But even if the fields are empty, the isset returns true.
Here is a simplified code: (name of the page: test.php, the post method is directed to itself)
if (isset($_POST['test'])) {
echo 'field set';
}
?>
<form method="post" action="test.php">
<input type="text" name="test"/>
<input type="submit" value="Submit"/>
</form>
When you submit, it always echoes 'field set', even if you didn't enter anything in the "test" input.
Thanks for your answers !
So check whether $_POST['test'] is empty or not and try like this
if (isset($_POST['test']) && trim($_POST['test']) != '') {
echo 'field set';
}
You can also use empty,but Note that "0" is also "empty" and along with it its better to trim the inputs to remove spaces. Another way is to check the length of input using strlen
if (isset($_POST['test']) && trim(strlen($_POST['test'])) > 0) {
echo 'field set';
}
try with empty() to check blank cause after submit form $_POST['test'] always found in isset() even it's blank. so need to check with empty()
if (!empty($_POST['test'])) {
echo 'field set';
}
0 would be empty/blank with checking empty()
You can do this with empty() method.try with this code.
ex : echo !empty($_POST['test']) ? "field set" : "" ;
It will echo that you field is set because all you are checking for if is the array key test exists with isset(). As long as you have a field in your form with the name test, it will create an empty key, thus resulting in it being set.
What you would rather like is to see if the key is set, and if the key also is empty or not.
This you can do by:
if( isset( $key['test'] ) && $key['test'] !== "" ){
//Do something
}
I have here PHP code that will check if the value is posted and I want to check its value in single line of code.
I have here a single code, I know that this is wrong because I always get the wrong value.
if(isset($_POST['handler_name']) == $h_name){
}
THANKS!
Here you go
if(isset($_POST['handler_name']) && ($_POST['handler_name'] == $h_name)) {
}
if(isset[$_POST['handler_name'])?(if([$_POST['handler_name'])?echo "equal":echo "not equal"):echo "not set";
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:
if (!empty($_GET['pID']) || !empty($_GET['pName']) || !empty($_GET['pDesc']) || !empty($_GET['pPrice']))
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:
replace OR with AND ( && )
use 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)
It's probably better that you switch the conditions around, like this:
if(empty($_GET['pID']) || empty($_GET['pName']) || empty($_GET['pDesc']) || empty($_GET['pPrice'])) {
echo "Please fill up all the values";
} else {
// Do other validation.
}
This way, you know your inputs are correct before you do anything else. Obviously I've not tested this, but it should work as expected. What you were saying before was asking if ANY of the inputs was not empty, do the additional validation. As one of the other commenters explained, if you wanted to do that, you should be using && rather than ||.
Changing it around just makes it a bit clearer!
It's a logic problem with your code. Using || in this situation means that if ANY of those inputs contains a value, then the first condition is met. What you want to do is AND, not OR, so that the first condition is only met if all of the inputs are !empty.
I don't remember for sure what the AND operator is for PHP, since it's been a long time, but it's probably &&.
The question already has been answered, but there is one more thing,
I recommend that you use $_POST instead of $_GET because $_POST is way more secure, as you use HTML Forms. You could look it up on internet.
Here is a link, the first answer says it all: Difference between $_POST & $_GET
This is just wrong
if (!empty($_GET['pID']) || !empty($_GET['pName']) || !empty($_GET['pDesc']) || !empty($_GET['pPrice'])){}
You need to make it like this:
if (!empty($_GET['pID'], $_GET['pName'], $_GET['pDesc'], $_GET['pPrice'])){}
And no need to make ORs at all. Also, you'd better check if any of the given values are empty and throw error on that.