Blank Field Detection - Problem

I am trying to detect in ajax what is an empty field. ( echo $field;→> output - radios)

This one if (!data.livre && data.livre == "radios") {checks to see if it is data.livreinvalid (this works) and now I want to check if the radio echo is or not. If this is a radio, I showed the user: empty radio stations

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 determine if an empty field is radio or age?

thank

+3
source share
1 answer

If the result could be that only the radio stations or age or none of them can be empty, you need to change your check to !data.livres || data.livres == "radios" || data.livres == "age". Then it will display a message if your json output is as follows:

{livres: false}

or

{livres: "radios"}

or

{livres: "age"}

Edit: Changes as per 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"
}
+2
source

All Articles