Reputation: 850
I have a php page where i have a hidden input as follows:
<input type="hidden" id="selOption'+(i+1)+'" name="selOption'+(i+1)+'" value="0"/>
As you can see that it has default value=0. Now using Ajax i'm sending value of the above input along with some other hidden input type="hidden" to another page like:
function showbtn(no,tid,tp){
var v_qno = document.getElementById("QuestionID"+no).value;
var v_secid = document.getElementById("sectionID"+no).value;
var v_selans = 0;
var v_vstatus = document.getElementById("visitStatus"+no).value;
v_selans = document.getElementById("selOption"+no).value;
var dataString = 'testId='+tid+'&secId='+v_secid+'&qNo='+ v_qno+'&selAns='+v_selans+'&vStat='+v_vstatus;
alert(dataString);
$.ajax({
type: "POST",
url: "../member/updtBttnStatus.php",
data: dataString,
cache: false,
success: function(result){
//some code
}
alert(result);
}
});
}
My problem is that in the updtBttnStatus.php page when i am using
if (!empty($_POST['selAns'])){
//some code here}
else{//}
to validate the value it goes to else part of the if loop whereas it should not be so because default value=0 being set in the first page.
Please help me as to what is that i am missing. Thanks in advance
Upvotes: 0
Views: 57
Reputation: 41885
No, you shouldn't use empty()
on a 0
. You'll get a false positive because in your case 0
is a valid value.
Quoting the manual:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
As an alternative, use isset()
in your if block instead:
if(isset($_POST['selAns']) && $_POST['selAns'] !== '') {
echo 'hi';
}
Upvotes: 3