0

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?

5
  • 1
    Where are you making your requests queries to? There is no URL in your calls. Note that your PHP should be in a PHP file, using a plugin to insert PHP via a shortcode will never work, is a dead end, and a massive security risk.
    – Tom J Nowell
    Dec 10, 2020 at 12:23
  • That's what I asked about in the last paragraph. Because I inserted php with the wp plugin, I didn't have to create a template for the page, therefore my understanding is that there is no physical page to refer to. Am I right? P.s: The reason why I did it this way, was because I am creating this code on the xampp localhost and I had a problem with the permissions for the php template
    – Mal_235
    Dec 10, 2020 at 12:26
  • 1
    There are APIs for AJAX request, a simple REST API endpoint can be put in a plugin or functions.php and give you a pretty URL to make requests to. However, because you've used that plugin to write your PHP your options are severely limited, so even if you could get AJAX working, which I think is highly unlikely, you wouldn't be able to do anything as there's no WordPress APIs available in that context. Your biggest problem right now is that you have a super dangerous PHP shortcode plugin installed. Any other issues you have are miniscule insects in comparison to this behemoth.
    – Tom J Nowell
    Dec 10, 2020 at 12:34
  • 1
    For clarification, right now it would be easier and safer to buy server space and do your work via CPanel than it would be to continue with the PHP shortcode plugin, I cannot emphasise enough how big a problem it is, and how much it will cause you problems. Fix your PHP template permission issues with XAMP, then look up the documentation for the register_rest_route function at developer.wordpress.org/reference/functions/register_rest_route, this has nothing to do with noconflict mode ( WP loads jQuery in no conflict mode already )
    – Tom J Nowell
    Dec 10, 2020 at 12:37
  • 1
    That will do. Thank you
    – Mal_235
    Dec 10, 2020 at 12:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.