即使在表单提交时也要检查数据

时间:2019-03-03 10:31:52

标签: javascript php jquery checkbox

我正在使用Javascript获取表单中的所有数据,因为所检查的数据可以更新或删除。

<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>

这是我使用的Javascript。

<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>

我的问题是,即使提交了表单,如何仍保持对数据的检查?有什么办法可以使用php吗?我对JSON不熟悉

0 个答案:

没有答案