Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
¿Por qué AJAX no pasa el valor del nombre del archivo html (parte JS) al archivo PHP usando POST?

Traté de pasar el valor del nombre del archivo .js a PHP usando el método POST. Pero cuando inicio el código, no hay ninguna alerta.
En la consola solo hay: Error de referencia no capturado: $ no está definido
¿Sabes qué se podría mejorar?

 <html> <body> <head> <script type="text/javascript"> const name = "asdasd"; $.ajax({ url: "TargetFile.php", method: "POST", data: { name }, success: function (result) { alert(result); }, error: function (error) { alert("Error " + error.status); } }) </script> </head> </body> </html>
 <?php $name = $_POST['name']; if (empty($name)) { echo "Name is empty"; } else { echo $name; } } ?>
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe cargar jQuery lib antes de usarlo. Y también la etiqueta principal debe estar fuera de la etiqueta del cuerpo.

 <html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script> <script type="text/javascript"> const name = "asdasd"; $.ajax({ url: "TargetFile.php", method: "POST", data: { name }, success: function (result) { alert(result); }, error: function (error) { alert("Error " + error.status); } }) </script> </head> <body> </body> </html>
about 4 years ago · Santiago Trujillo Report

0

Como un simple script de prueba de una sola página que enviará una solicitud POST al servidor que envía la respuesta para la devolución de llamada ajax. Como han dicho otros, debe incluir la biblioteca jQuery, pero debe cambiar la carga útil a un objeto literal válido. La solicitud POST espera pares de name=value como se indica a continuación.

 <?php if( $_SERVER['REQUEST_METHOD']=='POST' ){ $result=!empty( $_POST['name'] ) ? $_POST['name'] : 'Name is empty'; exit( $result ); } ?> <!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8' /> <title>Basic jQuery Ajax POST</title> <script src='//code.jquery.com/jquery-latest.js'></script> <script> const name = "Geronimo frequently kicked ass"; $.ajax({ url: location.href, //"TargetFile.php" method: "POST", data: { 'name':name }, success: function(result) { alert(result); }, error: function(error) { alert("Error " + error.status); } }) </script> </head> <body> </body> </html>

Usando 2 scripts separados, uno que hace la solicitud (index.html) y el otro que procesa la solicitud y envía la respuesta (targetfile.php)

índice.html

 <!DOCTYPE html> <html lang='en'> <head> <meta charset='utf-8' /> <title>Basic jQuery Ajax POST</title> <script src='//code.jquery.com/jquery-latest.js'></script> <script> const name = "Geronimo kicked ass"; $.ajax({ url: "TargetFile.php", method: "POST", data: { 'name':name }, success: function(result) { console.log( result ) }, error: function(error) { alert("Error " + error.status); } }) </script> </head> <body> </body> </html>

archivoobjetivo.php

 <?php if( $_SERVER['REQUEST_METHOD']=='POST' ){ $result=!empty( $_POST['name'] ) ? $_POST['name'] : 'Name is empty'; exit( $result ); } ?>

Ambos scripts en el mismo directorio, la solicitud se realiza correctamente y se muestra el mensaje de la consola (se eliminó la molesta alert )

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!