Error in jQuery, when sending FORM: Maximum call stack size exceeded

2
Uncaught RangeError: Maximum call stack size exceeded
    at xb (jquerymin.js:4)
    at xb (jquerymin.js:4)

Hello, I try to send the following form, and I receive this error by console:

<form id="form_comentarios" action="POST">
    <!--<input type="hidden" id="puntuacion" required>-->
    <input type="hidden" id="codigo_producto" value="<?php echo $producto['id']; ?>">
    <input type="text" id="nombre" placeholder="Nombre" style="width: 50%;" required><br>
    <input type="email" id="email" placeholder="Email" style="width: 50%;" required><br>
    <textarea id="comentario" placeholder="Escriba aquí su comentario" required></textarea><br>
    <input type="submit" id="submit" value="Comentar">
</form>

I receive the data in a .js file (which I put below):

$("#form_comentarios").submit(function(e){
  var puntuacion = $('#puntuacion').val()
  var codigo_producto = $('#codigo_producto').val()
  var nombre = $('#nombre').val()
  var cod_agente = $('#cod_agente').val()
  var fecha = $('#fecha').val()

  e.preventDefault();

  $.ajax({
    type: 'POST',
    url: 'php/setValoracion.php',
    data: {'puntuacion': puntuacion,
           'codigo_producto': codigo_producto,
           'nombre': nombre,
           'email': email,
           'comentario': comentario,
            }
  })
  .done(function(insertarComentario){
    alert('comentario enviado con éxito');
  })
  .fail(function(){
    alert('fallo');
  })
});

This is the PHP I'm trying to access:

function insertarComentario(){
    $puntuacion = $_POST['puntuacion'];
    $codigo_producto = $_POST['codigo_producto'];
    $nombre = $_POST['nombre'];
    $email = $_POST['email'];
    $comentario = $_POST['comentario'];

    $stmt = $con->prepare("INSERT INTO comentarios (id_prod, nombre_cliente, email, comentario) VALUES (?,?,?,?)");
    $stmt->bind_param('isss', $codigo_producto, $nombre, $email, $comentario);
    $stmt->execute();
    $stmt->close();
    $con->close();
}

echo insertarComentario();
    
asked by Javier Avila Fernandez 07.09.2018 в 17:22
source

1 answer

1

I think your problem is that you are passing some data as for example in your file js I do not see anywhere that you recive the comment and then send it in the data without being defined, I also wonder why the input hidden type is commented and you are receiving it in js and php , also in the data the comment has a comma at the end so the ajax stays waiting for more parameters to send.

then I propose this modification ...

 <form id="form_comentarios" action="POST">
    <input type="hidden" id="puntuacion" required>
    <input type="hidden" id="codigo_producto" value="<?php echo $producto['id']; ?>">
    <input type="text" id="nombre" placeholder="Nombre" style="width: 50%;" required><br>
    <input type="email" id="email" placeholder="Email" style="width: 50%;" required><br>
    <textarea id="comentario" placeholder="Escriba aquí su comentario" required></textarea><br>
    <input type="submit" id="submit" value="Comentar">
</form>

modified js file

     $(document).ready(function(){

    $("#form_comentarios").submit(function(e){
      var puntuacion = $('#puntuacion').val()
      var codigo_producto = $('#codigo_producto').val()
      var nombre = $('#nombre').val()
      var cod_agente = $('#cod_agente').val()
      var comentario = $('#comentario').val()
      var fecha = $('#fecha').val()

      e.preventDefault();

      $.ajax({
        type: 'POST',
        url: 'php/setValoracion.php',
        data: {'puntuacion': puntuacion,
               'codigo_producto': codigo_producto,
               'nombre': nombre,
               'email': email,
               'comentario': comentario
                }
      })
      .done(function(response){
     if(response=="ok"){
        alert('comentario enviado con éxito');
       }else{

           alert('comentario fallo');
      }
      })
      .fail(function(){
        alert('fallo');
      })
    });
})

modified php file

 $puntuacion = $_POST['puntuacion'];
    $codigo_producto = $_POST['codigo_producto'];
    $nombre = $_POST['nombre'];
    $email = $_POST['email'];
    $comentario = $_POST['comentario'];

    $stmt = $con->prepare("INSERT INTO comentarios (id_prod, nombre_cliente, email, comentario) VALUES (?,?,?,?)");
    $stmt->bind_param('isss', $codigo_producto, $nombre, $email, $comentario);
   if( $stmt->execute()){
     echo "ok";
    $stmt->close();

}else{
 echo "fail";
}
$con->close();

hopefully and be of your help, greetings!

    
answered by 07.09.2018 / 17:53
source