Pass form with multiple ids and select to PHP using AJAX
P粉729198207
P粉729198207 2024-02-26 14:17:13
0
1
411

I'm trying to collect selected checkboxes, update or delete them and pass this form to php via ajax. I'm very new to this and have spent a few days trying to figure out a way.

Web research didn't give me any results, I tried sending ajax a few different ways - no results. While using only PHP, I get the form with select and ids arrays.

How do I make it work with AJAX?

My form. It is placed inside the table. The body of the table is in the php file. There are other forms within the modal window in that document, just mentioning if there might be a conflict.

<div class="inner">
                <div class="option">
                        <div class="e-table">
                            <div class="table-responsive table-lg mt-3">
                                <form method="post" id="menu_form">
                                <table class="table table-bordered">
                                        <select name="select" id="menu" >
                                            <option value="select">Please select</option>
                                            <option value="set-active">Set active</option>
                                            <option value="set-not-active">Set not active</option>
                                            <option value="delete">Delete</option>
                                        </select>
                                    <thead>
                                        <tr>
                                            <th class="text-nowrap align-middle"><input type="checkbox" id="select-all"></th>
                                            <th>Name</th>
                                            <th>Role</th>
                                            <th>Status</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                <tbody id="users_data">
                                <input type="submit" value="submit" name="submit_btn" />

                                </tbody>
                                </table>
                                </form>

                            </div>
                        </div>
                </div>
            </div>

This is what I'm trying to do using AJAX.

<script type="text/javascript">
    $(document).ready(function() {
        $('#menu_form').on('submit', function (e) {
            e.preventDefault();

            $.ajax({
                type: 'post',
                url: "process.php",
                data: $('#menu_form').serialize(),
                success: function (response) {
                    console.log(response)
                }
            });
        });
    });

My PHP. The body of the table and handles form submission. Only the treatment for one of the options is shown so as not to make the question longer, the other options are pretty much the same.

<td class="text-nowrap align-middle"><input type="checkbox" name="update_id" value="<?= $row['id'] ?>"></td>
        <td class="text-nowrap align-middle"><?= $row['name']; ?></td>
        <td class="text-nowrap align-middle"><?= $row['role']; ?></td>
        <td class="text-center align-middle">
            <?php if ($row['status'] == 1): ?>
                <i class="fa fa-circle active-circle"></i>
            <?php else: ?>
                <i class="fa fa-circle not-active-circle" style="color:grey"></i>
            <?php endif; ?>
        </td>
        <td class="text-center align-middle"><button class="btn btn-sm btn-outline-secondary badge" type="button"
                    onclick="edit_record(<?= $row['id']; ?>)" >Edit</button>
            &nbsp;<button class="btn btn-sm btn-outline-secondary badge" type="button"
                    onclick="delete_record(<?= $row['id']; ?>)" ><i class="fa fa-trash"></i></button>
        </td>
    </tr>

if(isset($_POST['submit_btn'])) {
    $idArray = $_POST['update_id'];
    $idString = implode(',', $idArray);

    if (isset($_POST['select']) && $_POST['select'] == 'set-active') {
        $statement = $pdo->prepare("UPDATE user SET status = 1 WHERE id IN ($idString)");
        $result = $statement->execute();
        if ($result) {
            $response = array('status' => true, 'message' => 'Record edited successfully.');
        }else{
            $response = array('status' => true, 'message' => 'Error');
        }

        echo json_encode($response);    
    }

P粉729198207
P粉729198207

reply all(1)
P粉244730625

In my opinion, the problem lies here:

if(isset($_POST['submit_btn'])) {

The in the form is not part of the body/payload sent with the POST request (after all, it is a button and does not contain any information useful to the server).

Thus, the if() check above will always be false, and all code blocks inside the if() block will fail. will not be executed.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!