I’m changing sign in form on my site to use ajax. its just basically to check whether username and password are correct.But its not working at all it refreshes and nothing happens, can anyone see where I’m gong wrong in the code?
<html>
<body>
<div id="content">
<h1>Login Form</h1>
<form id="form1" name="form1" action="testform.php" method="post">
<p><label for="username">Username: </label>
<input type="text" name="username" id="username" /></p>
<p><label for="password">Password: </label>
<input type="password" name="password" id="password" /></p>
<p><input type="submit" id="login" name="login" /></p>
</form>
<div id="message"></div>
</div>
</body>
</html>
<script type="text/javascript">
<script type="text/javascript"
src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
$(document).ready(function() {
$("#login").click(function() {
var action = $("#form1").attr('action');
var form_data = {
username: $("#username").val(),
password: $("#password").val(),
is_ajax:
};
$.ajax({
type: "POST",
url: action,
data: form_data,
success: function(response)
{
if(response == 'success')
$("#form1").slideUp('slow', function() {
$("#message").html("<p class='success'>logged in!</p>");
});
else
$( "#message").html("<p class='error'>Invalid username and/orpassword</p>");
}
});
return false;
});
});
</script>
<?php
$is_ajax = $_REQUEST['is_ajax'];
if(isset($is_ajax) && $is_ajax)
{
$username = $_REQUEST['username'];
$password = $_REQUEST['password'];
if($username == 'to_difine' && $password == 'to_difine')
{
echo "success";
}
}
?>
Problems:
Corrections:
exit;to the php insideif is_ajaxso the rest of the html is not returned.<script type="text/javascript">below the jquery script.Working example and source.