0

I don't know how to put the value from a PHP variable into a jQuery variable. I tried some ways but still it doesnt work.

checkboxstatus = "<?php echo $checkboxstatus; ?>";

Everytime when I do this in database the row looks like:

enter image description here

jQuery(document).ready(function() {
  jQuery("#wyslij").click(function(e) {
    e.preventDefault();
    checkboxstatus = "<?php echo $checkboxstatus; ?>";

    jQuery.ajax({
      type: "POST",
      url: "czekboks.php",
      data: {
        checkboxstatus: checkboxstatus
      },
      success: function(result) {
        console.log('the data was successfully sent to the server');
      }
    })
  })
})
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
zirekx
  • 39
  • 4
  • 3
    For that to work you need to execute your jQuery/JS code in a PHP page, not a JS page, which is part of the reason it's not a good pattern to follow. A better idea would be to set the `$checkboxstatus` value as a data attribute on the `#wyslij` element and read that back in the click event without the need for any PHP spaghetti. – Rory McCrossan Sep 15 '20 at 15:53
  • I recommend looking up what the difference between client-side and server-side coding is. – HoldOffHunger Sep 15 '20 at 15:55
  • Thank you @RoryMcCrossan. :) – zirekx Sep 15 '20 at 16:03
  • you can't use php inside JS. You can do it the other way around tho. – amarinediary Sep 15 '20 at 17:53

1 Answers1

0

You can localize the PHP variable and then use it in the js file.