<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>
<th scope="col">Сумма</th>
<th scope="col">Комментарий</th>
<th scope="col">Курьер</th>
<th scope="col"> </th>
<?php
$post = get_delivery_users();
?>
<?php foreach ($post as $users): ?>
<form id="ajax-form">
<tbody>
<tr>
<th scope="row"><input type="hidden" name="form_id" value="<?=$users['id']?>"></th>
<td><div class="form-group">
<input class="form-control" type="date" name="delivery_date" value="<?=htmlspecialchars($users['delivery_date'], ENT_QUOTES)?>" id="example-date-input">
</div></td>
<td><select name="delivery_adress" class="form-control">
<? foreach(get_delivery_addresses() as $delivery_address): ?>
<option <?if ($delivery_address == $users['delivery_adress']) echo ' selected'?>><?=htmlspecialchars($delivery_address, ENT_QUOTES)?></option>
<? endforeach;?>
</select></td>
<td><input class="form-control" type="text" name="delivery_time" value="<?=htmlspecialchars($users['delivery_time'], ENT_QUOTES)?>" id="example-text-input"></td>
<td><input class="form-control" type="text" name="phone" value="<?=htmlspecialchars($users['phone'], ENT_QUOTES)?>" id="example-text-input"></td>
<td><input class="form-control" type="text" name="surname" value="<?=htmlspecialchars($users['surname'], ENT_QUOTES)?>"></td>
<td><input class="form-control" type="text" name="name" value="<?=htmlspecialchars($users['name'], ENT_QUOTES)?>"></td>
<td><input class="form-control" type="text" name="second_name" value="<?=htmlspecialchars($users['second_name'], ENT_QUOTES)?>"></td>
<td><input class="form-control" type="text" name="sum" value="<?=htmlspecialchars($users['sum'], ENT_QUOTES)?>"></td>
<td><input class="form-control" type="text" name="manager_comment" value="<?=htmlspecialchars($users['manager_comment'], ENT_QUOTES)?>"></td>
<td><div class="form-group">
<select name="courier" class="form-control">
<? foreach(get_couriers() as $couriers): ?>
<option <?if ($couriers == $users['courier']) echo ' selected'?>><?=htmlspecialchars($couriers, ENT_QUOTES)?></option>
<? endforeach;?>
</select>
</div></td>
<td><button type="submit" formmethod="post" name="couriers" id="pisos" class="btn btn-xs btn-warning mb-3">На доставку</button></td>
</tr>
</tbody>
</form>
<?php endforeach; ?>
</table>
А вот код, который выполняется в courier.php:
<?php
require_once('db.php');
if(isset($_POST['couriers']))
{
$form_id = (int)$_POST['form_id'];
$sum = (int)$_POST['sum'];
$manager_comment = strip_tags(trim($_POST['manager_comment']));
$phone = strip_tags(trim($_POST['phone']));
$name = strip_tags(trim($_POST['name']));
$surname = strip_tags(trim($_POST['surname']));
$second_name = strip_tags(trim($_POST['second_name']));
$manager_comment = strip_tags(trim($_POST['manager_comment']));
$delivery_adress = strip_tags(trim($_POST['delivery_adress']));
$delivery_time = strip_tags(trim($_POST['delivery_time']));
$delivery_date = strip_tags(trim($_POST['delivery_date']));
$courier = strip_tags(trim($_POST['courier']));
$SQL = "UPDATE Users SET phone='$phone', sum='$sum', name='$name', surname='$surname', second_name='$second_name', manager_comment='$manager_comment', delivery_adress='$delivery_adress', delivery_time='$delivery_time', delivery_date='$delivery_date', courier='$courier' WHERE id='$form_id'";
$result = mysqli_query($link, $SQL);
if ($result) {
header('Location:delivery.php');
}
else {
printf("Ошибка: %s\n", mysqli_error($link));
}
}
?>
Помогите, пожалуйста, составить корректный ajax-запрос для записи в бд данных из таблицы. Я не понимаю, как это делать, если все данные в цикле, как и кнопки
Пытался сделать таким образом, алерт выходит, но в бд ничего не пишется...
$(document).ready(function() {
$("#ajax-form").submit(function() {
$.ajax({
type: "POST",
url: "courier.php",
data: $(this).serialize()
}).done(function() {
alert("Данные сохранены");
});
return false;
});
});