I am working on a web project for the university, so I am dealing with AJAX for the first time.
Basically, I would like to create a platform for restaurant employees to manage orders, tables, reservations, and so on.
This is the window to add dishes to an order:
So far I have only interacted with my API by submitting forms. What I would like to achieve here, is that when I click on the + or – symbols, the related dish must be inserted in the database.
Below I will just put a few pieces of code that have to do with my problem.
update.js:
$(document).on("click", ".add-menuItem-button", function () {
var id_comanda = $(this).attr("data-id_comanda");
var id_menu = $(this).attr("data-id_menu");
$.ajax({
url: "http://localhost/projects/programmazione_web/api/dettagli_comanda/create.php",
type: "POST",
data: id_comanda, id_menu,
success: function (result) {
// added to order, notify
bootbox.alert("Aggiunto alla comanda");
},
error: function (xhr, resp, text) {
// show error to console
console.log(xhr, resp, text);
bootbox.alert("Errore");
}
});
The following are the PHP functions.
create.php:
// prepare object
$dettagli_comanda = new dettagliComanda($db);
// get id of record to be edited
$data = json_decode(file_get_contents("php://input"));
// make sure data is not empty
if (
!empty($data->id_comanda) &&
!empty($data->id_menu)
) {
// set property values
$dettagli_comanda->id_comanda = $data->id_comanda;
$dettagli_comanda->id_menu = $data->id_menu;
// create new record
if ($dettagli_comanda->create()) {
// set response code - 201 created
http_response_code(201);
// tell the user
echo json_encode(array("message" => "Operazione eseguita con successo."));
}
// if unable to add new record
else {
// set response code - 503 service unavailable
http_response_code(503);
// tell the user
echo json_encode(array("message" => "Non è stato possibile eseguire l'operazione, riprova più tardi."));
}
}
// if data is empty
else {
// set response code - 400 bad request
http_response_code(400);
// tell the user
echo json_encode(array("message" => "C'è stato un problema durante l'elaborazione della richiesta, controlla i dati e riprova."));
}
create function in the object.php file:
// create new record
function create()
{
// sql query
$query = "INSERT INTO dettagli_comanda
SET id_comanda = :id_comanda,
id_menu = :id_menu,
stato = '0'";
// prepare query
$stmt = $this->conn->prepare($query);
// sanitize
$this->id_comanda = htmlspecialchars(strip_tags($this->id_comanda));
$this->id_menu = htmlspecialchars(strip_tags($this->id_menu));
// bind values
$stmt->bindParam(":id_comanda", $this->id_comanda);
$stmt->bindParam(":id_menu", $this->id_menu);
// execute query
if ($stmt->execute()) {
return true;
}
return false;
}
I have tested PHP functions using Postman, and they work without errors.
I’m sure the problem is in the AJAX call, more specifically in passing values.
Anyone know how to help me?
Also, if I can somehow improve the consistency of my question, please let me know.
2
Answers
First of all, I thank @AttemptedMastery and @MagnussEriksson for their help and patience. It took a while, but I found a different solution, however their help was inspirational.
Basically, the problem is that the values stored in
var id_comanda = $(this).attr("data-id_comanda");
andvar id_menu = $(this).attr("data-id_menu");
are strings whilejson_decode(file_get_contents("php://input"));
accepts JSON content type.The solution is to use the
JSON.stringify()
function on the data inside the Ajax call as follows:You need to pass the params as an an object. See below:
Also, make sure you submit the content type below the
data
key like so: