ok now below is my code that when some body puts the username and password and click submit button then the Javascript and ajax is triggered
<html>
<head>
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
$(function () {
$('form').on('submit', function (e) {
$.ajax({
type: 'post',
url: 'redirect.php',
data: $('form').serialize(),
success: function () {
alert('form was submitted');
}
});
e.preventDefault();
});
});
</script>
</head>
<body>
<form>
<div class="add_list">
<p><span>Select User Name : </span>
<input type="text" name="username" size="20" />
</p>
<p><span>Select Your Password : </span>
<input type="password" name="password" size="20" />
</p>
</div>
<div class="add_user">
<input type="submit" value="add user" />
</div>
</form>
</body>
</html>
after it is triggered it loads this page which is redirect.php page
<?php
include 'includes\config.php';
//connects to the database
if (!empty($_POST['username']) && !empty($_POST['password'])) {
// Now checking user name and password is entered or not.
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$check = "SELECT * from user where username= '" . $username . "'";
$qry = mysql_query($check);
$num_rows = mysql_num_rows($qry);
if ($num_rows > 0) {
// Here we are checking if username is already exist or not.
echo "The username you have entered already exist. Please try again with another username.";
echo ('<script type="text/javascript"> alert ("The username you have entered already exist. Please try again with another username.");</script>');
exit;
}
// Now inserting record in database.
$query = "INSERT INTO user VALUES ('','" . $username . "','" . $password . "')";
//id,username and password
mysql_query($query);
echo "User Added Successfully";
echo ('<script type="text/javascript"> alert ("The username is added successfully.");</script>');
exit;
}
?>
now the problem is when i submit the form it alerts that form is submitted but the actual javascript which is in redirect.php is not loading that is not alerting and also the echo of php is also not working though it is getting submitted in database and another problem is that if that username already exists when a person is entering the form is submitted is alerting and then nothing and when i check it is not getting submitted as username already exist but not echo or alert of javascript is working.