jeudi 23 avril 2015

AJAX Passing value to PHP

I am having a problem understanding how to have a checkbox which when clicked, sends a value to a php file which then performs a function with that value... any help?

The checkbox is called in a while loop on each result, each result has it's own ID, when the user checks the box, it sends that results ID and the checkbox value to the php file...

I have this script :

<script>
$("input.live-checkbox]").click(function() {
// store the values from the form checkbox box, then send via ajax below
var check_live = $(this).is(':checked') ? 1 : 0;
var check_id = $(this).attr('value');

    $.ajax({
        type: "POST",
        url: "makelive.php",
        data: {id: check_id, live: check_live},
        success: function(){
            $('form#liveForm').hide(function(){$('div.success').fadeIn();});

        }
    });
return true;
});
</script>

and this is the checkbox in the HTML :

if (isset($live)){

    echo '<input type="checkbox" name="live-checkbox" id="live-checkbox" value='.$id.' checked>';

} else {

echo '<form name="liveForm" id="liveForm"><input type="checkbox" name="live-checkbox" id="live-checkbox" value="live"><input type="hidden" name="id" id="id" class="form-control" value='.$id.'></input></form>';
}

When this value is passed to the php it is inserted into the table, I then need the checkbox is be displayed as on or off depending if this value exists in the database.




Aucun commentaire:

Enregistrer un commentaire