I can not get the value from ajax to php and then make an insert

-1

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)]);

Result database:

    
asked by Yasiel Hernández 04.04.2018 в 09:51
source

3 answers

0

I think you do not pass the data correctly using ajax. First of all check that PHP receives the expected information in $fechaEntrada and in $fechaSalida . If nothing comes, the error may be in the following:

var fechaEntrada = document.getElementById('fechaEntrada').value;
var fechaSalida = document.getElementById('fechaSalida').value;

    $.ajax({
        type: "POST",
        url: "php/insertar.php",
        data:{fechaEntrada,fechaSalida}, // <<<--- ERROR
        dataType:"html",
        asycn:false, //el error que cometí de sintaxis, es async
        success: function(){
           alert("Ha sido ejecutada la acción.");
        }

Where marked "ERROR" these passed an "object" of the json style. There always has to be "key": "value".

The correct thing would be to always pass something similar to:

{
  "clave1":"valor",
  "clave2":"valor"
}

Another thing would be if you wanted to pass an array:

{
  "indice":["valor1", "valor2", "valor3", ... ] 
}

To correctly pass the information:

var infoParaEnviar: {entrada: fechaEntrada, salida: fechaSalida};

$.ajax ({
    type: "POST",
        url: "php/insertar.php",
        data: infoParaEnviar,
        dataType:"text",
        asycn:false, //el error que cometí de sintaxis, es async
        success: function(){
           alert("Ha sido ejecutada la acción.");
        }
});

In PHP you only have to change:

$fechaEntrada=$_POST['entrada'];
$fechaSalida=$_POST['salida'];

Try it and tell me. I hope it helps you.

    
answered by 04.04.2018 / 10:10
source
1

You have to pass a JSON object.

var fechaEntrada = document.getElementById('fechaEntrada').value; //probar .valueAsDate
var fechaSalida = document.getElementById('fechaSalida').value; // probar .valueAsDate

$.ajax({
    type: "POST",
    url: "php/insertar.php",
    data: { entrada: fechaEntrada, salida: fechaSalida }
})
 <input type="datetime" name="fechaEntrada" id="fechaEntrada">
 <input type="datetime" name="fechaSalida" id="fechaSalida">

Otherwise PHP should work.

    
answered by 04.04.2018 в 10:15
0

SOLVED The problem was when making the insert to the database PHP:

    <?php
$fechaEntrada=$_POST['entrada'];
$fechaSalida=$_POST['salida'];

    $ins = new PDO("mysql:dbname=reservas;host=127.0.0.1","root","");
        $insert=$ins->prepare("INSERT INTO 'registro' ('entrada', 'salida') VALUES (CAST('". $fechaEntrada ."' AS DATE),CAST('". $fechaSalida ."' AS DATE) )");
        $insert->execute();
    ?>

AJAX / JavaScript provided by @Jonathan in the comments:

var fechaEntrada = document.getElementById('fechaEntrada').value;
    var fechaSalida = document.getElementById('fechaSalida').value;

    var infoParaEnviar=
    {
        entrada: fechaEntrada,
        salida: fechaSalida
    };

    $.ajax({
        type: "POST",
        url: "php/insertar.php",
        data: infoParaEnviar,
        dataType: "text",
        asycn: false, //el error que cometí de sintaxis, es async
        success: function () {
            alert("Ha sido ejecutada la acción.");
        }
    });
    
answered by 04.04.2018 в 15:24