I am developing simple program in html which inserts the data into MySQL DB using php script. But every time I press Save button it redirects me to php page. I have created Javascript file but its not working for me. Please help
HTML FILE (index.html)
<html>
<head>
<script src="js/jquery-1.11.0.min.js" type="text/javascript"></script>
<script src="js/my_script.js" type="text/javascript"></script>
</head>
<body>
<form id="myForm" action="demo_insert.php" method="post">
Name : <input type="text" name="name"><br />
Email : <input type="text" name="email"><br />
Domain : <input type="text" name="domain"><br />
Role : <input type="text" name="role"><br />
<button id="sub">Save</button>
</form>
<span id="result"></span>
</body>
</html>
php file
<?php
$conn = mysql_connect('localhost', 'root', '');
$db = mysql_select_db('test');
$name=$_POST['name'];
$domain=$_POST['domain'];
$email=$_POST['email'];
$role=$_POST['role'];
$sql = "INSERT INTO user20015 VALUES ('$name', '$domain', '$email', '$role')";
if(mysql_query($sql))
echo "Successfully Inserted";
else
echo "Insertion Failed";
?>
my_script.js File
$("#sub").click( function() {
$.post( $("#myForm").attr("action"),
$("#myForm :input").serializeArray(),
function(info){ $("#result").html(info);
});
clearInput();
});
$("#myForm").submit( function() {
return false;
});
function clearInput() {
$("#myForm :input").each( function() {
$(this).val('');
});
}