Answer the question
In order to leave comments, you need to log in
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>
<?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));
}
}
?>
<div class="alert alert-primary" role="alert">
<strong>Heads up!</strong> This alert needs your attention, but it's not super important.
</div>
$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>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question