Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

377
Vistas
Pasar una variable PHP a un archivo php usando AJAX

Estoy usando este código para mi sección de comentarios. aquí todo funciona, excepto cuando intento enviar la variable "$post['id']" a través de AJAX al archivo "fetch-comment.php" y aparece este error:

 Warning: Undefined array key "load" in C:\xampp\htdocs\x\fetch-comment.php on line 5 Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ' parent='0' ORDER BY id DESC' at line 1 in C:\xampp\htdocs\x\fetch-comment.php:10 Stack trace: #0 C:\xampp\htdocs\x\fetch-comment.php(10): PDOStatement->execute() #1 {main} thrown in C:\xampp\htdocs\x\fetch-comment.php on line 10

aquí está mi código JS:

 $(document).ready(function() { $('#comment_form').on('submit', function(event) { event.preventDefault(); var form_data = $(this).serialize(); $.ajax({ url: "../add-comment.php", method: "POST", data: form_data, dataType: "JSON", success: function(data) { if (data.error != '') { $('#comment_form')[0].reset(); $('#comment_message').html(data.error); } } }) }); load_comment(); function load_comment() { var load = <?php echo $post['id']; ?>; $.ajax({ url: "../fetch-comment.php", method: "POST", data: load, success: function(data) { $('#display_comment').html(data); } }) } $(document).on('click', '.reply', function() { var comment_id = $(this).attr("id"); $('#comment_id').val(comment_id); $('#comment_name').focus(); $('#comment_content').attr('placeholder', 'Write Your Reply !'); }); });

y aquí está la parte del archivo php que arroja el error:

 <?php include "config/db.php"; $load = $_POST['load']; $parent = 0; //when parent is zero it means it's original comment and not a reply $comments = $conn->prepare('SELECT * FROM comments WHERE post=?, parent=? ORDER BY id DESC'); $comments->bindValue(1, $load); $comments->bindValue(2, $parent); $comments->execute(); $comments = $comments->fetchAll(PDO::FETCH_ASSOC);

He estado jugando con el código durante 10 horas seguidas, ¡ayuda por favor!

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

¿No debería ser

 data : {list : load}

en la llamada ajax y

 $load = $_POST['list'];

en el script php. No veo ninguna list en la matriz $_POST enviada por la llamada ajax.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda