1

im bit confused how to save checkbox value dynamically in 1 table!

database:

id  PERSON   check
--------------------
1   Person1  1
2   Person2  1
3   Person3  1
4   Person4  0

HTML code:

<form id="availability">
Person1 <input type="checkbox" id="person1" value="1" checked>
Person2 <input type="checkbox" id="person2" value="1" checked>
Person3 <input type="checkbox" id="person3" value="1" checked>
Person4 <input type="checkbox" id="person4" value="0">
</form>

jquery:

$('#availability').submit(function(){
  $.post('include/setting.php', {
      person1: $('#person1').val(),
      person2: $('#person2').val(),
      person3: $('#person3').val(),
      person4: $('#person4').val(),
  }, function(data){

    if(data.success) {

    } else {

    }
  }, 'json');
  return false;
})

PHP:

$person1 = $_POST['person1'];
$person2 = $_POST['person2'];
$person3 = $_POST['person3'];
$person3 = $_POST['person4'];
$db->query("UPDATE person SET id='???");
4
  • 1
    Why are you saving the value and not the checked status? If you want checked status you can use $('#person1').is(':checked'); Commented Mar 11, 2011 at 15:55
  • i want save/update multiple checkbox (if the checked or not) value to database Commented Mar 11, 2011 at 15:56
  • as far as I know, the value of your checkbox isn't the checked status, it's the value. Do you want to save the checked status? Commented Mar 11, 2011 at 15:57
  • yeap! if its checked or not check the value will be 1 or 0 so both to save Commented Mar 11, 2011 at 15:59

3 Answers 3

3

Your approach isn't the cleanest or most modular, but it can work. Here are direct modifications to your code which should help it out:

HTML

<form id="availability">
    Person1 <input type="checkbox" id="person1" value="1">
    Person2 <input type="checkbox" id="person2" value="1">
    Person3 <input type="checkbox" id="person3" value="1">
    Person4 <input type="checkbox" id="person4" value="1">
</form>

javascript

$( '#availability' ).submit( function()
{
    $.post(
        'include/setting.php',
        {
            persons: {
                person1: ( $( '#person1:checked' ).length ? 1 : 0 ),
                person2: ( $( '#person2:checked' ).length ? 1 : 0 ),
                person3: ( $( '#person3:checked' ).length ? 1 : 0 ),
                person4: ( $( '#person4:checked' ).length ? 1 : 0 )
            }
        },
        function( data )
        {
            if( data.success )
            {

            }
            else
            {

            }
        },
        'json'
    );

    return false;
} );

PHP

<?php
foreach( $_POST['persons'] as $personIdentifier => $checked )
{
    //DO PROPER CHECKS AND ESCAPING OF INCOMING DATA!!!!!!!
    $db->query( "UPDATE person SET check = {$checked} WHERE PERSON = '{$personIdentifier}'" );
}
Sign up to request clarification or add additional context in comments.

7 Comments

thanks for your help. i tried your code, its not updating the database value :( any idea why?
Without access to your server to verify all info you gave us and troubleshoot, no, I don't have an idea.
are you available for a chat. so i cud give you as many as i can if u ok :) chat.stackoverflow.com/rooms/11/php
sorry i was there waiting for u too before u came :D. anyway i sorted it out.. thanks for ur help tho ;)
@RyanOscar I couldn’t tell you. It’s where the OP was sending the data. The question was front-end related, so I didn’t ask
|
3

You do not need a form that processes all of the inputs at one time, but only send a request for the modified field without using any form.

$('input:checkbox').change(function() {
  $.post('include/setting.php', {
    id: this.id,
    value: $(this).attr("checked")?1:0
  }, function(data){
    if(data.success) {
    } else {
    }
  }, 'json');
  return false;  
});

and for PHP:

$db->query("UPDATE person SET value=".$_POST['value']." WHERE  id=".$_POST['id'] );

Also remember to always escape queries with mysql_real_escape_string()

4 Comments

This definitely makes sense, but only if the OP intends to have the value changed on every check or uncheck, which may not be the case.
then what does mean "how to save checkbox value dynamically" ?
@justkt thanks. something like i wanted. i followed your code, but it doesnt update the database. it didnt pick up the id
Yes, that's my answer ! I don't understand why updating all users on modification. If there was 1000 persons ?
2

To pass a 0 if unchecked and a 1 if checked, you can use the is method with the :checked selector to determine the checked value as a boolean then use the ternary operator to set your JSON value to 1 or 0 depending on the result.

$('#availability').submit(function(){
  $.post('include/setting.php', {
      "person1" : $('#person1').is(':checked') ? 1 : 0,
      "person2" : $('#person2').is(':checked') ? 1 : 0,
      "person3" : $('#person3').is(':checked') ? 1 : 0,
      "person4" : $('#person4').is(':checked') ? 1 : 0,
  }, function(data){

    if(data.success) {

    } else {

    }
  }, 'json');
  return false;
})

2 Comments

thanks! and how do i pass it to php? and save the value 0 and 1?
You are already posting it to your PHP file, correct? See if your PHP file is receiving the correct data, first, then iterate through and save as you've shown above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.