I have started learning AJAX today. I wrote a simple ajax call, but instead of getting response asynchronously, it just load the PHP script page. Just like normal form submission.
html
<form id="form" method="POST" action="name.php">
<input type="text" name="Name">
<input type="submit" value="Submit" />
JS
$("#form").submit(function(e) {
e.preventDefault();
var url = "name.php";
$.ajax({
type: "POST",
url: url,
data: $("#form").serialize(),
success: function(data)
{
alert(data);
}
});
});
PHP
<?php
$data= $_POST['Name'];
echo $data;
?>
Instead of getting Alert on the same page, I am being redirected to name.php
with the value I have submited.