0

This is part of a code to update or delete data in the database. And I used jQuery Validate to check the data validity works correctly when updating the data, but validate also works when deleting data. I have to complete the fields that are empty first to be able to delete. Can you suggest a solution that validates only when updating the data?

<?php
//update
if (isset($_POST['update'])) {
        $wpdb->update(
                $table_name, //table
                array('start_date' => $start_date,),
                array('ID' => $id), //where
                );}
//delete
    else if (isset($_POST['delete'])) {
        $wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE id = %s", $id));
    } else {//selecting value to update 
        $schools = $wpdb->get_results($wpdb->prepare("SELECT * from $table_name where id=%s", $id));
        foreach ($schools as $s) {
            $start_date = $s->start_date;
        }
    }
    if (isset($_POST['delete'])) {
    else if (isset($_POST['update'])) {
    } else { ?>
    <form class="form-group" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
                    <input type="date" name="start_date" value="<?php echo $start_date; ?>" class="ss-field-width" required />
        <input type='submit' name="update" value='Save' class='button'> &nbsp;&nbsp;
        <input type='submit' name="delete" value='Delete' class='button' onclick="return confirm('Are you sure?')">
    </form>
    <?php } ?>

</div>
<script>
jQuery("form").validate({
    errorPlacement: function(error, element) {
        error.insertAfter(element.parent("label"));
        console.log(error[0].innerText);
    },
    showErrors: function(errorMap, errorList) {
        console.log({
            errorMap,
            errorList
        });
        this.defaultShowErrors();
    }
});
</script>
<?php

}
Sparky
  • 98,165
  • 25
  • 199
  • 285
  • Put `formnovalidate="formnovalidate"` inside the submit button where you do not want validation. See the answer in the linked duplicate above. – Sparky Oct 12 '22 at 02:01

0 Answers0