html code:
<input type="datetime" name="fechaEntrada" id="fechaEntrada">
<input type="datetime" name="fechaSalida" id="fechaSalida">
Ajax:
var fechaEntrada = document.getElementById('fechaEntrada').value;
var fechaSalida = document.getElementById('fechaSalida').value;
$.ajax({
type: "POST",
url: "php/insertar.php",
data: {fechaEntrada, fechaSalida},
dataType: "html",
asycn: false, // el error que cometí de sintaxis, es async
success: function() {
alert("Ha sido ejecutada la acción.");
}
});
PHP:
$fechaEntrada = $_POST['fechaEntrada'];
$fechaSalida = $_POST['fechaSalida'];
$ins = new PDO("mysql:dbname=reservas;host=127.0.0.1", "root", "");
$insert = $ins->prepare("INSERT INTO `registro` (`entrada`, `salida`) VALUES (?, ?)");
$insert->execute([date_parse($fechaEntrada), date_parse($fechaSalida)]);
Database result:
You have to pass a JSON object.
Otherwise PHP should work.
I think you are not passing the data correctly via ajax. Check first of all that PHP gets the information expected in
$fechaEntrada
and in$fechaSalida
. If nothing arrives, the error may be in the following:Where it is marked as "ERROR" you are passing an "object" of the json style. There always has to be "key":"value".
The correct thing would be to always pass something similar to:
Another thing would be if you wanted to pass an array:
To correctly pass the information:
In PHP you would only have to change:
Try it and tell me. I hope it helps you.
SOLVED The problem was when doing the insert to the PHP database:
AJAX/JavaScript provided by @Jonathan in the comments: