dimanche 3 mars 2019

Keep data checked even on form submit

I am getting all the data in the form using Javascript because the checked data can either be updated or deleted.

<table cellpadding="1" cellspacing="1" id="resultTable">
    <tr>
        <th>Availability</th>
        <th>Item</th>
    </tr>
   <?php
        if(mysqli_num_rows($query) > 0){
        while($row = mysqli_fetch_assoc($query)){
        extract($row);
        ?>
    <tr>
        <td><input type="checkbox" name="selected_id[]" class="checkbox" value="<?php echo $id; ?>" /></td>
        <th><?php echo $item; ?></th>
    </tr>
    <?php } }else{ ?>
        <tr><td colspan="3">No records found.</td></tr> 
    <?php } ?>
    <form name="updateForm" method="post" action="updateproduct.php">
        <select name="availability" id="availability" required onchange="enableButton()">
           <option disabled="disabled" selected="selected" value=''>Select an option.</option>
           <option value="Available today">Available today</option>
           <option value="Not available today">Not available today</option>
        </select>
     </form>
     <input type="submit" id="btn_update" value="Update Records" /> 
     <input type="submit" id="btn_delete" value="Delete Records" />
</table>

<form name="deleteForm" method="post" action="deleteselected.php" onsubmit="return deleteAlert();"></form>

This is the Javascript that I've used.

<script type="text/javascript">
                        function enableButton()
                        {
                            var selectelem = document.getElementById('availability');
                            var btnelem = document.getElementById('btn_update');
                            btnelem.disabled = !selectelem.value;
                        }   
                        function setFormValuesAndSubmit(form) {
                            const checked = $("#resultTable input[name='selected_id[]']:checked");
                            if (!checked.length) {
                                alert("Please select an item.");
                                return;
                            }

                            form = $(form);
                            form.find("[name='selected_id[]']").remove();
                            checked.each(function(){
                                $("<input/>", {type: "hidden", name: "selected_id[]", value: $(this).val()}).appendTo(form);
                            });
                            form.submit();
                        }
                        $("#btn_update").click(function() {
                            setFormValuesAndSubmit(document.updateForm);
                        });
                        $("#btn_delete").click(function(){
                            setFormValuesAndSubmit(document.deleteForm);
                        });
</script>

My question is how can I keep the data checked still checked even when the form is submitted?




Aucun commentaire:

Enregistrer un commentaire