How to pass value from ajax to php in no conflict mode?

Question

I am trying to use ajax to pass value of the checklist to php in wordpress. I am using plugin to insert php on a page as a shortcode. Then inside the theme I created a js file with js/ajax code. I tested the console appearing on the checkbox click and it responds, but my ajax doesn’t do what it supposed to do.
This is my checkbox (HTML) inserted into the page with plugin:

<input type='checkbox' class='checkboxes' id='myID' name='myName' value='myValue'>

My script inside the theme

var $jq = jQuery.noConflict();
$jq(document).ready(function() {
  $jq('.checkboxes').click(function() {
    selected_checkbox= $jq(this).attr('id');  
    var checkBox = document.getElementById(selected_checkbox);

    if (checkBox.checked == true){
      var is_checked = "checked";
                        
      $jq.ajax({
        type: "POST",
        data:{
          selected_checkbox: selected_checkbox, 
          is_checked: is_checked
        }
      });

    }else{
      var is_checked = "unchecked";

      $jq.ajax({
        type: "POST",
        data:{
          selected_checkbox: selected_checkbox, 
          is_checked: is_checked
        }
      });
    }
  });
});

And my php to call for the ajax result (again inserted on the page with a plugin)

if( $_POST['is_checked']=='checked' ){
  $selected_checkbox=$_POST['selected_checkbox'];
  echo $selected_checkbox;
  exit;
}

Do I need URL set up (if so, what will the url be for the page ‘localhost/myxeample/mycheckbox’? as this confuses me in wp)? Or since javascript works on the destination page ajax should pass the value too?

0
Mal_235 2 months 0 Answers 5 views 0

Leave an answer