M
M
Maxim2019-04-02 09:33:45
AJAX
Maxim, 2019-04-02 09:33:45

How to do ajax on the button to write to the database and display the string in a different color?

Good afternoon.
I have a table that outputs values ​​from mysql in a loop.
Here's what it looks like:

<table class="table text-center">
                                            <thead class="text-uppercase">
                                            <tr>
                                                <th scope="col">ID</th>
                                                <th scope="col">Метро</th>
                                                <th scope="col">Время</th>
                                                <th scope="col">ФИО</th>
                                                <th scope="col">Сумма</th>
                                                <th scope="col"> </th>
                                                <th scope="col"> </th>
                                                <th scope="col"> </th>
                                                </tr>
                                            </thead>
                                            <?php
                                            $post = get_cargo_users1();
                                            ?>
                                            <?php foreach ($post as $users): ?>
                                            <form method="post" action="dost.php">
                                                <tbody>
                                                <tr>
                                                    <td><?=$users['id']?></td>
                                                    <td><?=htmlspecialchars($users['delivery_adress'], ENT_QUOTES)?></td>
                                                    <td><?=htmlspecialchars($users['delivery_time'], ENT_QUOTES)?></td>
                                                    <td><?=htmlspecialchars($users['surname'], ENT_QUOTES)?> <?=htmlspecialchars($users['name'], ENT_QUOTES)?> <?=htmlspecialchars($users['second_name'], ENT_QUOTES)?></td>
                                                    <td><?=htmlspecialchars($users['sum'], ENT_QUOTES)?></td>
                                                    <td><button type="submit" formmethod="post" name="delivered" class="btn btn-xs btn-success mb-3" id="delivered">Отдал</button></td>
                                                    <td><button type="submit" formmethod="post" name="undelivered" class="btn btn-xs btn-danger mb-3">Отказ</button></td>
                                                    <td><button type="button" formmethod="post" name="delivery"  class="btn btn-xs btn-warning mb-3" data-toggle="modal" data-target="#exampleModalLong">Перенос</button></td>
                                                     </tr>
                                                </tbody>
                                            </form>
                                         <?php endforeach ?>
                                        </table>

What I'm trying to do:
1. By pressing the delivered and undelivered buttons, write their values ​​to the database.
Here is the code that handles them:
<?php

require_once('db.php');

if(isset($_POST['undelivered']))
{
    $form_id = (int)$_POST['form_id'];
    $SQL = "UPDATE Users SET delivery_status=2 WHERE id='$form_id'";
    $result = mysqli_query($link, $SQL);

    if ($result) {
        header('Location:cargo.php');
    }
    else {
        printf("Ошибка: %s\n", mysqli_error($link));
    }
}

if(isset($_POST['delivered']))
{
    $form_id = (int)$_POST['form_id'];
    $SQL = "UPDATE Users SET delivery_status=1 WHERE id='$form_id'";
    $result = mysqli_query($link, $SQL);

    if ($result) {
        header('Location:cargo.php');
    }
    else {
        printf("Ошибка: %s\n", mysqli_error($link));
    }
}

if(isset($_POST['perenos']))
{
    $form_id = (int)$_POST['form_id'];
    $delivery_adress = strip_tags(trim($_POST['delivery_adress']));
    $delivery_time = strip_tags(trim($_POST['delivery_time']));
    $delivery_date = strip_tags(trim($_POST['delivery_date']));
    $SQL = "UPDATE Users SET delivery_status=3, delivery_adress='$delivery_adress', delivery_time='$delivery_time', delivery_date='$delivery_date' WHERE id='$form_id'";
    $result = mysqli_query($link, $SQL);
    if ($result) {
        header('Location:cargo.php');
    }
    else {
        printf("Ошибка: %s\n", mysqli_error($link));
    }
}

?>

2. Make a line with values ​​in a different color.
There are already ready-made patterns for changing colors, for example:
<div class="alert alert-primary" role="alert">
                                            <strong>Heads up!</strong> This alert needs your attention, but it's not super important.
                                        </div>

I'm trying to make it so that if the value delivered=1, then the line is displayed with this div, if 2 - with another, 3 - with one more, if there is no value, then it remains as it is. I tried to do it through a ternary operator, but I did not fully understand how.
$class = ($row['delivery_status'] == 1)? 'alert-primary' : '';

<div class="alert <?=$class?>" role="alert">
                                            <strong>Heads up!</strong> This alert needs your attention, but it's not super important.
                                        </div>

Please help me to understand this stupid :(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
ThunderCat, 2019-04-02
@ThunderCat

https://www.google.ru/search?q=example+ajax+request

M
Maxim, 2019-04-02
@ikfah012

In general, by trial and error, I came to a working version myself:
$('button[name="couriers"]').on('click', function() {
let $row = $(this).closest('tr ');
let data = $row.find('input, select').serialize();
$.ajax({
type: "POST",
url: "courier.php",
data: data
}).done(function () {
alert("Data saved");
});
return false;
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question