I've got 2 web-pages named test1.html and test1.php. I'm sending some data as key/value pairs using ajax .post() method, but it doesn't seem to reach the server side as I'm getting the following error:
Warning: Undefined variable $name in C:\xampp\htdocs\WebMid2Practice\ajax\test1.php on line 6
Warning: Undefined variable $age in C:\xampp\htdocs\WebMid2Practice\ajax\test1.php on line 7
Here's my code:
test1.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>
test1.php
<?php
if (isset($_REQUEST['name']) && isset($_REQUEST['age'])) {
$name = $_REQUEST['name'];
$age = $_REQUEST['age'];
}
echo "Name entered is: " .$name;
echo "Age: " .$age;
?>