Intenté imprimir los datos que ingresó el usuario y transferirlos a través de ajax e intenté imprimirlos o repetirlos con la ayuda del código php, pero no pude hacerlo. enter code here
este es mi código:
`<html> <head> <title> test ajax </title> <script src = "jquery.js"></script> </head> <body> <h2>test button</h2><br> <form> <input type = "text"id = "a"><br> <input type = "button" id = "b" value = "display"> </form> <script> $(document).ready(function(){ $('#b').click(function() { let a = $('#a').val(); alert(a); $.ajax({ type: "POST", url: "same_file.php", data: {c: a}, success: function() { } }); }); }); </script> </body> </html> <?php extract($_POST); if(isset($_POST["c"])) { echo $_POST["c"]; } ?>
`
Creo que tengo el problema con el código php. por favor dame alguna solución.
Pruébalo así
este es tu html
<html> <head> <title> test ajax </title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <h2>test button</h2><br> <form> <input type = "text" id = "a" > <br> <input type = "button" id = "b" value = "display" > </form> </body> </html> <script> $(document).ready(function(){ $('#b').click(function() { let a = $('#a').val(); //alert(a); $.ajax({ type: "POST", url: "same_file.php", data: {c: a}, success: function(response) { alert(response); } }); }); }); </script>
Este es su mismo_archivo.php
<?php //extract($_POST); code works even without this if(isset($_POST["c"])) { echo $_POST['c']; } ?>
No estoy seguro de si su llamada a la biblioteca jquery está bien, así que llamé a la de la web. Además, su éxito: función () no estaba haciendo nada, por lo que no recibió nada.
Además, formatee siempre su código con alguna sangría para que pueda ser más fácil de leer y comprender por otros.