Reputation: 1129
I have the following code inside a form whereby when press submit button, php side is grabbing the input value using $_POST['revisionCad']
.
<form method="POST">
<input type="number" class="form-control" placeholder="Revision" name="revisionCad" id="revisionCad" min="0">
<button name="submit" class="btn btn-primary" id="submit" tabindex="5" value="Save" type="submit" style="width:200px;">Create/Change Revision</button>
</form>
My Issue
Everything working fine except when I gave number 0 as input, $_POST
is getting no value. My error check telling, please enter a number.
Below is my error check code
if(isset($_POST['revisionCad'])){
$revisionCad = $_POST['revisionCad'];
}
if(empty($revisionCad)){
erDisplay('Please select a revision for Cadgui');
}
But any number greater than 0, $_POST['revisionCad']
is grabbing correctly. I tried to remove the min
attribute as well as change to -1. But still whenever I enter number 0, $_POST['revisionCad']
not grabbing any value. Anyone knows why?
Upvotes: 4
Views: 3540
Reputation: 9373
You need to change the condition as below:
if(isset($revisionCad) && $revisionCad >=0){
erDisplay('Please select a revision for Cadgui');
}
Actually empty() function treats 0 as empty value so it gives this error.
Upvotes: 1
Reputation: 734
Add a default value to your form or change it in your code dynamically as follow:
<form method= "POST">
<!-- <input type="number" class="form-control" placeholder="Revision" name="revisionCad" id="revisionCad" min="0"> -->
/** Assign default value or set it automatically in your code.**/
<input type="number" class="form-control" placeholder="Revision" name="revisionCad" id="revisionCad" min="0" value=0>
<button name="submit" class="btn btn-primary" id="submit" tabindex="5" value="Save" type="submit" style="width:200px;">Create/Change Revision</button>
</form>
Your PHP codes:
if(isset($_POST['revisionCad'])){
$revisionCad = $_POST['revisionCad'];
}
if(empty($revisionCad)){
erDisplay('Please select a revision for Cadgui');
}
Should be changed to :
// You can set a default value of 0 in the code or leave it at null;
$revisionCad = (isset($_POST['revisionCad']) && !empty( $_POST['revisionCad'] ) || $_POST['revisionCad'] >=0 ) ?
trim($_POST['revisionCad']) : null;
/* Check if data is set to default null by your script,
* Display error, since no data was received
*/
if(is_null($revisionCad))
{
erDisplay('Please select a revision for Cadgui');
}
// if no error, continue with the rest of the codes ...
I hope this helps.
Upvotes: 1
Reputation: 26460
To explain why this is happening, you have to take a closer look at the PHP empty()
function. From the documentation we see that (emphasis mine)...
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE.
In PHP, the value of zero is a falsy value. So you get the following output,
var_dump(false == 0); // Outputs "true"
var_dump(empty(0)); // Outputs "true"
So you need to alter your code to check if the variable is set and greater or equal to zero, which you can do by altering the snippet you showed to the following.
if (isset($_POST['revisionCad']) && $_POST['revisionCad'] >= 0) {
$revisionCad = $_POST['revisionCad'];
} else {
erDisplay('Please select a revision for Cadgui');
}
Upvotes: 5
Reputation: 171
Try (!isset($revisonCad))
. I hope it works.
Since you have given empty()
function, it treat 0 as empty value. Hence, no input is grabbed.
Upvotes: 0
Reputation: 812
you have to give 0 in value property as default value.
<input type="number" class="form-control" placeholder="Revision" name="revisionCad" id="revisionCad" min="0" value="0">
Upvotes: 0