plugin development – Validate data only when updating the database

Question

hope you had a good day.

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 and 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.
Thanks for your help.

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

0
Fardad Farhang 2 months 2022-10-11T01:47:20-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse