I am trying detect in ajax what is the field that is emp开发者_如何学运维ty. (echo $field;
>>> output is radios)
This if (!data.livre && data.livre == "radios") {
checks if data.livre
is not true (that is working) and now i want to check if the echo is radios or not. If is radios i showed to the user: empty radios
function check($form) {
$fields = array("radios", "age");
foreach($fields as $field) {
if(empty($form[$field])) {
echo $field;
return false;
}
}
return true;
}
$data = array();
$data['livre'] = $val-> check($form);
echo json_encode($data);
JSON output:
radios{"livre":false}
js
success: function(data) {
if (!data.livre && data.livre == "radios") { //problem here
$("#msgbox1").fadeTo(200, 0.1, function() {
$(this).html('empty radios').addClass('messageboxerror1').fadeTo(900, 1);
});
}
}
EDIT: What is the best way to identify if the field that is empty is radios or age ?
thanks
If the result can be that only radios or age or none of both can be empty then you have to edit your check to !data.livres || data.livres == "radios" || data.livres == "age"
. It will then print the message if your json output is the following:
{livres: false}
or
{livres: "radios"}
or
{livres: "age"}
Edit: Changes according to your comment
if (!data.livre) { //problem here
// Do whatever you want to do when livres is false
} else if (data.livre == "radios") {
// Do whatever you want to do when livres is "radios"
} else if (data.livre == "age") {
// Do whatever you want to do when livres is "age"
}
精彩评论