Pass php checkbox variable to jQuery

I am a new user to jQuery and am struggling trying to pass php variable to jQuery. I am using a checkbox in a while loop so because there can only be 1 id, I suffixed the id with $ticket, ie; id=“check$ticket”. I need to get this value in jQuery for processing. After hours of trying to figure it out, I throw myself at the experts to show me the way. I would be grateful if someone could point out my error or correct my code to grab this value. Thanks

PHP Code

while($row = mysql_fetch_array($result)) 
    $ticket = $row['ticket_frm'];
    $rowdate = date("d/m/Y",strtotime($row['date_frm']));
    $id = $row['id_frm'];
    $from = $row['from_frm'];
    $subject = $row['subject_frm'];
    $message = $row['message_frm'];
    $myString = $myString . "<span><input id=\\"check$ticket\\" class=\\"check\\" type=\\"checkbox\\" name=\\"delete\\" value=\\"$ticket\\"></span>";
    $myString = $myString . "<div class=\\"msgTrue buttonMailTrue\\" data-message=\\"$message\\" data-subject=\\"$subject\\" data-rowdate=\\"%s\\" data-from=\\"%s\\">";
    $myString = $myString . "<img src=\\"images/sml_new_mail_icon.gif\\" class=\\"mailIcon\\" alt=\\"\\" />$subject";
    $myString = $myString . "<div class=\\"rowdate\\">$rowdate</div><br />";
    $myString = $myString . "<span class=\\"mailFrom\\">$from</span>";
    $myString = $myString . "</div>";
      echo $myString; 
      echo '<p class="checked">'.'</p>'; 

jQuery code

$("#check" + TICKET_NUMBER_HERE).click(function() {
   var isChecked = $(this).prop("checked");  // or $(this).prop("checked")
     if (isChecked) {
       $("p.checked").html("Checkbox is checked: <b>True</b>");
   } else {
       $("p.checked").html("Checkbox is checked: <b>False</b>");

You can easily achieve the desired result by targeting the start of the id which would be check, see the below which would work fine.

$(function() {
    $('input[id^=check]').on('click', function() {
        $('.checked').html('Checkbox is checked: <strong>' + ($(this).is(':checked') ? 'True' : 'False') + '</strong>');

Excellent Chris. One more question. How can I get value of id to pass to db? Thanks

How were you planning on sending the value to the database?

  • via an Ajax request?
  • or via a form request?

preferably, ajax but would be willing to try either.

Ok so what I will suggest is you head over to the jQuery site via the below link and have a read about jQuery.ajax() and see how far you get, if you get to a point where you get completely stuck just let me know and I would be more then happy to help.

Thanks Chris

I am looking at the jquery site now chris with regard to ajax, but I seem to be having a problem getting more than 1 value with your code example. For instance, if there are more than 1 checkboxes then the click only gets the value of each checkbox. I need to find a way so if there are 3 checkboxes and a user clicks say 2, I need to get values from both checkboxes. Can you help with this? Thanks

I tried following code, bot still only showing 1 value when clicked.

$('.checked').show().html('Checkbox is checked: ' + $(this).val()).map(function(){ return $(this).val(); }).get().join(",");