I try to insert data through the ajax, PHP and jquery but the code is not working properly. anyone can help me what is going wrong. even I used serialize() method also but no result.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<form id="myform">
<label>Username</label> <input name="name" type="text">
<label>Password</label> <input name="password" type="text">
<button type="submit" name="submit" id="submit">Save</button>
</form>
<script>
$(document).ready(function() {
$("#submit").click(function(event) {
event.preventDefault();
$.ajax({
url: "new.php",
type: 'POST',
data: $("#myform").serialize(),
success: function(data) {
alert(data);
$('form').trigger("reset");
}
});
});
});
</script>
</body>
</html>
And this is the new.php it means the PHP code in another file.
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'root');
define('DB_PASSWORD', 'root');
define('DB_DATABASE', 'test');
$db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
if(isset($_POST['submit'])) {
$username=$_POST['name'];
$password=$_POST['password'];
$sql="INSERT INTO ourteam(name, position) VALUES ('$username','$password')";
$result=mysqli_query($db,$sql);
?>