Why does INSERT INTO not show me the data of the variable?

0

Hi, I'm trying to make a query in my php calendar by clicking, for now I'm doing fine.

  <left class="coordinar_left">
    <select id="tipoEventos">
      <option selected disabled>-Editar...-</option>
      <option value="Vacaciones_de_Pascua">Vacaciones de Pascua</option>
      <option value="Vacaciones_de_Navidad">Vacaciones de Navidad</option>
      <option value="Dias_festivos">Dias festivos</option>
      <option value="inicio_de_curso">Inicio de curso</option>
    </select><textarea id="seleccionar_fecha" name="fecha_texto" hidden></textarea>
  </left>

and the php:

<?php
   $conexion= new mysqli("sql113.260mb.net","n260m_20445422","jyEsRoXZ","n260m_20445422_profesorado");
 if (!$conexion) {
   echo "error al conectar con la base de datos";
 }
 $fecha1 = $_POST["fecha_texto"];

 $sql = "INSERT INTO vacaciones_pascua (fecha) VALUES ('".$fecha1."')";
 if($conexion->query($sql)===TRUE){
   echo "1";
 }else{
   echo "0";
 }
 $conexion->close();
?>

The problem is that I try to print the $ sql variable to see if it executes well and I get this: INSERT INTO vaca_pascua (date) VALUES ('');

that is, I do not get the value I put in the input

When I click on a calendar cell, it prints me in a textarea with the date and that I would have to save in the DB.

But I do not know why it is.

Thank you.

    
asked by cat_12 02.07.2018 в 15:27
source

1 answer

0

I already have the solution, the failures were several.

In the HTML I did not have a form tag, which would fit me like this:

<form method="POST" id="form_fecha"><textarea id="seleccionar_fecha" name="seleccionar_fecha" hidden></textarea></form>

In the jQuery script I made some modifications: old:

<script>
$(document).ready(function(){ 
    $("body").on('click', '.dates li', function(){
        var fecha = $("#form_fecha").serialize();
        $("#seleccionar_fecha").text($(this).attr('id'));
        setTimeout(function(){
          $.ajax({
             type:"POST",
             url:"guardar_vacaciones.php",
             success:function(i){
                 if (i==1) {
                     alert("correcto"); 
                 }else{ 
                     alert("error"); 
                 } 
             } 
        },1000); 
    });
}); 

updated:

<script>
$(document).ready(function(){
  $("body").on('mouseenter', '.dates li', function() {
    $("#seleccionar_fecha").text($(this).attr('id'));
  });
  $("body").on('click', '.dates li', function(){
    var fecha = $("#form_fecha").serialize();
      $.ajax({
        type:"POST",
        url:"guardar_vacaciones.php",
        data:fecha,
        success:function(i){
          if (i==1) {
            alert("enviado");
          }else{
            alert("error");
          }
        }
      });
      return false;
  });
});
</script>

For which I added a return false to not load the page when clicking on a box in the calendar and I also put a date variable that takes the name fields.

in PHP:

<?php
$conn = new mysqli("sql113.260mb.net","n260m_20445422","jyEsRoXZ","n260m_20445422_profesorado");
// Check connection
if (!$conn) {
    die("Error: " . $conn->connect_error);
} 
$fecha12= $_POST['seleccionar_fecha'];
$sql = " INSERT INTO vacaciones_pascua (fecha) VALUES ('$fecha12')";
echo mysqli_query($conn,$sql);
?>

First I did not put the table correctly, it was not 1esoacalendar but vacacion_pascua and at the end I have to print the mysqli_query ($ conn, $ sql), so that it prints 1 or 0, and the other, that is to say the POST was well called .

Finally it would look like this:

HTML:

    <form method="POST" id="form_fecha"><textarea id="seleccionar_fecha" name="seleccionar_fecha" hidden></textarea></form>

jQuery:

    <script>
$(document).ready(function(){
  $("body").on('mouseenter', '.dates li', function() {
    $("#seleccionar_fecha").text($(this).attr('id'));
  });
  $("body").on('click', '.dates li', function(){
    var fecha = $("#form_fecha").serialize();
      $.ajax({
        type:"POST",
        url:"guardar_vacaciones.php",
        data:fecha,
        success:function(i){
          if (i==1) {
            alert("enviado");
          }else{
            alert("error");
          }
        }
      });
      return false;
  });
});
</script>

PHP:

<?php
$conn = new mysqli("sql113.260mb.net","n260m_20445422","jyEsRoXZ","n260m_20445422_profesorado");
// Check connection
if (!$conn) {
    die("Error: " . $conn->connect_error);
} 
$fecha12= $_POST['seleccionar_fecha'];
$sql = " INSERT INTO vacaciones_pascua (fecha) VALUES ('$fecha12')";
echo mysqli_query($conn,$sql);
?>

Thanks for your collaboration

    
answered by 02.07.2018 в 19:25