简体   繁体   中英

Checkbox onChange submit form

What i have is a array of items from my mySQL database and each item has a checkbox. i am trying to make it so when you click on the checkbox it will submit the information to the database for the item that got checked or unchecked. have i have it is unchecked = 1 and checked = 0. this is for where i want to display the item.

Now my issue is I can't seem to get anything to submit into my database, I don't understand jQuery enough to be able to write a function for it, so i need some help. here is what i got for my code.

if(isset($_POST['submit'])){
        foreach($_POST['id'] as $id){
            $value = (isset($_POST['location'][$id]) && $_POST['location'][$id]=="0" ? '0' : '1');
            $insert = mysql_query("UPDATE items SET location='$value' WHERE id='$id'") or die('Insert Error: '.mysql_error());
        }
    }
echo '<form id="form1" method="post"><input type="submit" name="submit" value="Submit">';
$result = mysql_query("SELECT * FROM items")
    or die("Query Failed: ".mysql_error());
    $counter = 0;
    echo '<div class="specialcontainer">';
while($row = mysql_fetch_array($result)){
    list($id, $item_info, $item_img, $price, $sale, $location) = $row;
    if($location == '0'){
        $set_checked = ' checked="checked" ';
    }else{
        $set_checked = '';
    }
    if($counter % 5==0) {
        echo '</div>';
        echo '<div class="specialcontainer">';
    }
    echo '<div class="special"><img src="../images/items/'.$item_img.'" width="130" /><br />';
    echo $item_info.'<br />';
    echo 'Reg. $'.$price.'<br />';
    echo 'Sale $'.$sale.'<br />';
    echo 'Slide Show: <input type="checkbox" id="ch" value="0" name="location['.$id.']"'.$set_checked.' /><br />';
    echo '<input type="button" value="Edit" name="edit" onclick="window.location.href=\'specials.php?action=edit&id='.$id.'\'">';
    echo '<input type="button" value="Delete" name="Delete" onclick="window.location.href=\'specials.php?action=delete&id='.$id.'\'">';
    echo '<input type="hidden" name="id[]" value='.$id.' />';
    echo '</div>';
    $counter++;
}
echo '</div>';
echo '<input type="submit" name="submit" value="Submit"></form>';

so as you can see, i do have the submit button there, but my plan is to remove it for the onChange submit. I've tried the onchange="this.form.submit();" in the checkbox parameter but it don't work properly. so i just want it to submit anytime a checkbox gets clicked on kinda thing.

Would an Ajax solution like this work?

$('ch').click(function() {
    //this is what goes into $_POST
    var data = 'id='+ $(this).attr('name') +'&checked=' + $(this).is(':checked');
    $.ajax({
        //This would be the url that would handle updating the MySQL Record
        url: my_mysql_handler.php,
        cache: false,
        type: 'POST',
        data: data,
        success: function(response) {
            alert(response); //or whatever you want to do with the success/fail notification
        }
    });
});

您应该在复选框的onchange方法中尝试document.getElementById('form1').submit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM