Tengo 2 páginas web llamadas test1.html y test1.php . Estoy enviando algunos datos como pares clave/valor usando el método ajax .post(), pero parece que no llega al lado del servidor porque recibo el siguiente error:
Advertencia: variable no definida $nombre en C:\xampp\htdocs\WebMid2Practice\ajax\test1.php en la línea 6
Advertencia: Variable no definida $edad en C:\xampp\htdocs\WebMid2Practice\ajax\test1.php en la línea 7
Aquí está mi código:
prueba1.html
<!DOCTYPE html> <html> <head> <title>Ajax</title> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" ></script> <script> $(document).ready(function() { $("#load").click(function() { var txtName = $("#name").val(); var txtAge = $("#age").val(); $.post("test1.php", {name:txtName, age:txtAge}, function(data) { $("#result").html(data); }); }) }); </script> </head> <body> <h1>Ajax Practice</h1> <div class="result"></div> Enter Name: <input type="text" id="name"><br> Enter age: <input type="text" id="age"><br> <button id="load">Send request to test1.php</button> </body> </html>prueba1.php
<?php if (isset($_REQUEST['name']) && isset($_REQUEST['age'])) { $name = $_REQUEST['name']; $age = $_REQUEST['age']; } echo "Name entered is: " .$name; echo "Age: " .$age; ?>