problem when sending data with ajax to mysql

1

good day I have this small problem and I would like to give me a hand I am trying to send data to my database through ajax and I work all messages and all alerts and when I send to send says that everything was sent OK but the data is not added to the database. Then the code to see where I'm wrong.

this is the form

<div class="col-lg-4">
  <h1 class="page-header"><a id="jugador-add" class="btn btn-outline btn-primary">Añadir Jugador</a></h1>
</div>
<!-- COMIENZO AJAX AGREGAR JUGADOR  -->
<!-- comienzo Modal-->            
<div class="modal fade" id="myModalj" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="row">
        <div class="col-lg-12">
          <div class="panel panel-default">
            <div class="panel-heading">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">Añadir Jugador</h4>
            </div>
            <div class="modal-body">
              <div class="row">
                <form  method="post" id="forminsertar" name="forminsertar" role="form">
                  <div class="col-lg-6">
                    <div class="form-group">
                      <label>Nombre del Jugador</label>
                      <input class="form-control" placeholder="Escribir Nombre del Jugador"name="jNombre" id="jNombre">
                    </div>
                    <div class="form-group">
                      <label>Apellido del Jugador</label>
                      <input class="form-control" placeholder="Escribir Apellido del Jugador"name="jApellido" id="jApellido">
                    </div>
                    <div class="form-group">
                      <label>Fecha de Nacimiento</label>
                      <input class="form-control" name="jFecha" id="jFecha" type="date" required="required">
                    </div>
                    <div class="form-group">
                      <label>Cedula del Jugador</label>
                      <input class="form-control" placeholder="Escribir Cedula del Jugador"name="jCedula" id="jCedula">
                    </div>
                    <div class="form-group">
                      <label>Direccion del Jugador</label>
                      <input class="form-control" placeholder="Escribir Direccion del Jugador"name="jDireccion" id="jDireccion">
                    </div>
                    <div class="form-group">
                      <label>Ciudad del Jugador</label>
                      <input class="form-control" placeholder="Escribir Ciudad del Jugador"name="jCiudad" id="jCiudad">
                    </div>
                    <div class="form-group">
                      <label>Telefono del Jugador</label>
                      <input class="form-control" placeholder="Escribir Telefono del Jugador"name="jTelefono" id="jTelefono">
                    </div>
                  </div>
                  <div class="col-lg-6">
                    <div class="form-group">
                      <label>Estatura del Jugador</label>
                      <input class="form-control" placeholder="Escribir Estatura del Jugador"name="jEstatura" id="jEstatura">
                    </div>
                    <div class="form-group">
                      <label>numero del Jugador</label>
                      <input class="form-control" placeholder="Escribir Numero Chaqueta del Jugador"name="jNumero" id="jNumero">
                    </div>
                    <div class="form-group">
                      <label>Equipo</label>
                      <select name="jrefEquipo" class="form-control" id="jrefEquipo">
                        <option value="0" selected>Sin Equipo</option>
                        <?php do { ?>
                        <option value="<?php echo $row_DatosEquipo["idEquipo"]?>" ><?php echo $row_DatosEquipo["eNombre"]?></option>
                        <?php               
                          } while ($row_DatosEquipo = mysqli_fetch_assoc($DatosEquipo)); 
                          ?>
                      </select>
                    </div>
                    <div class="form-group">
                      <label>Posicion</label>
                      <select name="jrefPosicion" class="form-control" id="jrefPosicion">
                        <option value="0" selected>Sin Posicion</option>
                        <?php do { ?>
                        <option value="<?php echo $row_DatosPosicion["idPosicion"]?>" ><?php echo $row_DatosPosicion["pPosicion"]?></option>
                        <?php               
                          } while ($row_DatosPosicion = mysqli_fetch_assoc($DatosPosicion)); 
                          ?>
                      </select>
                    </div>
                    <div class="form-group">
                      <label>Email</label>
                      <input class="form-control" placeholder="Escribir Email"name="jEmail" id="jEmail">
                    </div>
                    <div class="form-group">
                      <label>Contraseña</label>
                      <input class="form-control" placeholder="Escribir Contraseña"name="jContraseña" id="jContraseña">
                    </div>
                  </div>
                </form>
              </div>
              <div  class="modal-footer">
                <!--bottones-->
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button> 
                <button id="aceptar_jugador" type="button" class="btn btn-primary">Aceptar</button>   
              </div>
              <!--bottones-->    
            </div>
            <!-- /.row (nested) -->
          </div>
        </div>
        <!-- /.panel -->
      </div>
    </div>
    <!-- final Cabecera del modal-->
  </div>
  <!-- final Modal content-->
</div>
<!-- final Modal-->

this is the ajax

<script >

    $(document).ready(function(){

    $("#jugador-add").click(function () {
        $("#myModalj").modal();





         $("#aceptar_jugador").unbind('click');
        $("#aceptar_jugador").click(function () {


        var jNombre = document.getElementById("jNombre").value;
        var jApellido = document.getElementById("jApellido").value;
        var jFecha = document.getElementById("jFecha").value;
        var jCedula = document.getElementById("jCedula").value;
        var jDireccion = document.getElementById("jDireccion").value;
        var jCiudad = document.getElementById("jCiudad").value;
        var jTelefono = document.getElementById("jTelefono").value;
        var jEstatura = document.getElementById("jEstatura").value;
        var jNumero = document.getElementById("jNumero").value;
        var jrefEquipo = document.getElementById("jrefEquipo").value;
        var jrefPosicion = document.getElementById("jrefPosicion").value;
        var jEmail = document.getElementById("jEmail").value;
        var jContraseña = document.getElementById("jContraseña").value;

        if ( jNombre == "" || jApellido == "" || jFecha == "" || jCedula =="" || jDireccion =="" || jCiudad =="" || jTelefono =="" || jEstatura =="" || jNumero =="" || jEmail =="" || jContraseña =="") {
        alert("Error: Todos los campos son obligatorios.");

      } else {
           $("#myModalj").modal('toggle');

        var params = {jNombre, jApellido, jFecha, jCedula, jDireccion, jCiudad, jTelefono, jEstatura, jNumero, jrefEquipo, jrefPosicion, jEmail, jContraseña}

         $.ajax({
        type: 'POST',
        url: 'jugador-insert.php',
        data: params,
        async:true,

          success: function(respuesta) {

            alert("Informacion: Jugador creado correctamente!");
            location.reload();
               }

            });
            }
          });
        });
    });

    </script>

and this is the insert

<?php require_once('../Conexion/conexion.php');?>
<?php 



    $jNombre = GetSQLValueString($_POST['jNombre'], "text"); 
    $jApellido = GetSQLValueString($_POST['jApellido'], "text");
    $jFecha = GetSQLValueString($_POST['jFecha'], "date");
    $jCedula = GetSQLValueString($_POST['jCedula'], "int");
    $jDireccion = GetSQLValueString($_POST['jDireccion'], "text");
    $jCedula = GetSQLValueString($_POST['jCedula'], "int");
    $jTelefono = GetSQLValueString($_POST['jTelefono'], "int");
    $jEstatura = GetSQLValueString($_POST['jEstatura'], "int");
    $jNumero = GetSQLValueString($_POST['jNumero'], "int");
    $jrefEquipo = GetSQLValueString($_POST['jrefEquipo'], "int");
    $jrefPosicion = GetSQLValueString($_POST['jrefPosicion'], "int");
    $jEmail = GetSQLValueString($_POST['jEmail'], "text");
    $jContra = GetSQLValueString(md5($_POST['jContraseña']));

    $jregistro=date('Y-m-d');


    $sql="INSERT into tbljugadores (jNombre, jApellido, jFecha,  jCedula, jDireccion, jCiudad, jTelefono, jEstatura, jNumero, jrefEquipo, jrefPosicion, jEmail, jContraseña, jregistro)
            values ('$jNombre','$jApellido','$jFecha','$jCedula','$jDireccion','$jCiudad','$jTelefono','$jEstatura','$jNumero','$jrefEquipo','$jrefPosicion','$jEmail','$jContra','$jregistro')";
    echo mysqli_query($con,$sql);
 ?>

everything works fine up there but it does not save the data to the database.

    
asked by user93054 05.07.2018 в 02:54
source

1 answer

0

The code in the server will not find anything in the POST, in your variable params of Javascript you must put each value like this:

clave : valor

that is, to the left what would be the key that you will use in the POST to obtain the value: $_POST["clave"] will give you the data that is in valor .

So, you should write params like this:

    var params = {
                    jNombre: jNombre, 
                    jApellido: jApellido, 
                    jFecha: jFecha, 
                    jCedula: jCedula, 
                    jDireccion: jDireccion, 
                    jCiudad: jCiudad, 
                    jTelefono: jTelefono, 
                    jEstatura: jEstatura, 
                    jNumero: jNumero, 
                    jrefEquipo: jrefEquipo, 
                    jrefPosicion: jrefPosicion, 
                    jEmail: jEmail, 
                    jContraseña: jContraseña
                }

Yes, the POST will have its data, each associated with the key on the left.

As a recommendation, avoid the use of variables with ñ or accented words, I say this in the case of jContraseña ... sometimes there are coding problems and the use of variables like this can affect the code.

Note on security: Your query is vulnerable to SQL injection attacks. Consider applying prepared queries to avoid it.

    
answered by 05.07.2018 в 03:58