samedi 10 juillet 2021

Can't Check Checkbox Value With Php and AJAX

I have a form and I use one checkbox. Thanks to the Checkbox, a new field is added and the user fills it.

I ran into a problem with everything going perfectly. The checkbox keeps returning empty, which completely destroys my if else structure.

Checkbox HTML Code:

<input type="checkbox" class="custom-control-input headerElementDrop" id="header_element_drop" name="header_element_drop" onchange="valueChanged()">

AJAX Code:

function headerElementInsert() {

  var headerElementAdd = $("#headerElementAdd").serialize();

  $.ajax({
    type: "POST",
    data: headerElementAdd,
    url: "actions/header-element-add.php",
    success: function (insertSuccess) {
      if ($.trim(insertSuccess) == "null") {

        //

      } else if ($.trim(insertSuccess) == "success") {

        //

      } else if ($.trim(insertSuccess) == "error") {

        //

        })

      }
    }
  });

}

header-element-add.php code:

if ($_POST) {


    $header_element_name = post('header_element_name');
    $header_element_icon = post('header_element_icon');
    $header_element_link = post('header_element_link');

    $header_element_drop = (isset($_POST["header_element_drop"])) ? 1 : 2 ;

    $header_element_drop_class_name = post('header_element_drop_class_name');
    $header_element_drop_elements = post('header_element_drop_elements');


    if($header_element_drop == 1) {

        if(!$header_element_name || !$header_element_drop || !$header_element_drop_class_name || !$header_element_drop_elements) {
            echo "null";
        }else{

            $header_element_drop_class = replace_tr($header_element_drop_class_name);

            $header_elements = $db->prepare("INSERT IGNORE INTO header_elements SET header_element_name=?, header_element_icon=?, header_element_link=?, header_element_drop=?, header_element_drop_class_name=?, header_element_drop_class=?, header_element_drop_elements=?");
            $insert = $header_elements->execute(array($header_element_name,$header_element_icon,$header_element_link,$header_element_drop,$header_element_drop_class_name,$header_element_drop_class,$header_element_drop_elements));
            
            if($insert) {
                echo "success";
            }else{
                echo "error";
            }

        }

    }else{

        if(!$header_element_name) {
            echo "null";
        }else {

            $header_elements = $db->prepare("INSERT IGNORE INTO header_elements SET header_element_name=?, header_element_icon=?, header_element_link=?");
            $insert = $header_elements->execute(array($header_element_name,$header_element_icon,$header_element_link));

            if($insert) {
                echo "success";
            }else{
                echo "error";
            }

        }

    }

}

I tried many ways to solve this problem but none of them worked. I would really appreciate if you could help.




Aucun commentaire:

Enregistrer un commentaire