Создание списка покупок с помощью SQL/PHP/JSON
Мне неоходим помощь в создании небольшой таблицы в SQL c помощбю JSON. Сайт одностраничный, но состоит из нескольких файлов PHP/JS и HTml/CSS.
<main>
<input type="text" id="item" required>
<input type="number" id="quantity" min="1" required>
<input type ="button" id="addButton" value="Add">
<span id="target">
</span>
</main>
JS
window.addEventListener("load", function() {
function success(a) {
let span = document.getElementById("target");
span.innerHTML = "hi";
}
let button = document.getElementById("addButton");
button.addEventListener("click", function() {
let item = document.getElementById("item").value;
let quantity = document.getElementById("quantity").value;
let url = "php/update.php?item=" + item +"&quantity=" + quantity;
console.log(url);
fetch(url, {credentials: 'include'})
.then(response => response.json())
.then(success)
});
});
И PHP
<?php
include "connect.php";
$item = filter_input(INPUT_GET,"item", FILTER_SANITIZE_SPECIAL_CHARS);
$quantity = filter_input(INPUT_GET, "quantity", FILTER_SANITIZE_NUMBER_INT);
echo "$item";
$check = false;
if(
$item !== null && $item !== "" &&
$quantity !== null && $quantity !== ""
) {
$check = true;
$command = "INSERT into shoppinglist (item, quantity) VALUES (?, ?)";
$stmt = $dbh->prepare($command);
$params = [$item, $quantity];
$success = $stmt->execute($params);
}
?>
- Как мне с помощью JSON добавить данные в таблицу?
- Как мне взять данные из таблицы и добавить их на туже страницу?(сверху остаются поля для ввода новых продуктов), а под ними при нажатии кнопки отображается все содержимое страницы?
Спасибо