SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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

    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

    Code:
    $(function(){
    $("#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>");
    	 }
    	 });
    });

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

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

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent Chris. One more question. How can I get value of id to pass to db? Thanks

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ploppy View Post
    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?

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    preferably, ajax but would be willing to try either.

  6. #6
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,191
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    http://api.jquery.com/jQuery.ajax/

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Chris

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried following code, bot still only showing 1 value when clicked.

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


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •