gpt4 book ai didi

php + jquery + mysql + 表单操作

转载 作者:太空宇宙 更新时间:2023-11-03 12:31:00 25 4
gpt4 key购买 nike

我正在使用 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.";
}


?>

最佳答案

替换

<input type = "submit" name = "submit" id = "name" value = "send feedBack" />

通过

<input type = "submit" name = "submit" id = "submit" value = "send feedBack" />

注意:id

此外,您应该在 (form).submit(); 而不是 ('submit').click(); 上触发事件

关于php + jquery + mysql + 表单操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15379508/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com