我正在使用 php 和 jquery 创建一个表单,以便在不刷新页面的情况下将数据插入数据库,但问题是页面刷新并将我引导到 php 页面任何人都可以帮助我
索引.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>feedback page</title>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link rel ="stylesheet" href = "css/default.css" />
<script type = "text/javascript">
$(function(){
$('#submit').click(function(){
$('#container').append('<img src = "img/loading.gif" alt="Currently loading" id = "loading" />');
var name = $('#name').val();
var email = $('#email').val();
var comments = $('#comments').val();
console.log(name, email, comments);
return false;
});
});
</script>
</head>
<body>
<form action = "submit_to_db.php" method = "post">
<div id = "container">
<label for = "name">Name</label>
<input type = "text" name = "name" id = "name" />
<label for = "email">Email address</label>
<input type = "text" name = "email" id = "email" />
<label for = "comments">Comments</label>
<textarea rows = "5"cols = "35" name = "comments" id = "comments"></textarea>
<br />
<input type = "submit" name = "submit" id = "name" value = "send feedBack" />
</div>
</form>
</div>
</body>
</html>
submit_to_db.php
<?php
$conn = new mysqli('localhost', 'root', 'root', 'my_db');
$query = "INSERT into comments(name, email, comments) VALUES(?, ?, ?)";
$stmt = $conn->stmt_init();
if($stmt->prepare($query)){
$stmt->bind_param('sss', $_POST['name'], $_POST['email'], $_POST['comments']);
$stmt->execute();
}
if($stmt){
echo "thank you .we will be in touch soon";
}
else{
echo "there was an error. try again later.";
}
?>