gpt4 book ai didi

php - 使用 AJAX 将表单值提交到数据库

转载 作者:行者123 更新时间:2023-11-29 11:48:06 25 4
gpt4 key购买 nike

我目前正在制作这份调查问卷,其中为用户提供了四种可供选择的答案。每个答案都分配有一个单选按钮,并且单选按钮具有数据库表中的值(从 1 到 10)。这样不同的选择就有不同的值(value)。用户还可以在问题之间来回切换。

完成八个问题后,考生将看到一个按钮,该按钮取代了下一个和后退按钮。当用户单击它时,它应该提交 AJAX 请求并将表单值发送到数据库。

我面临的问题是,当我单击按钮时,表单值没有发送到我的数据库。我已经测试过,表单显示答案,并且单选按钮具有值,因此问题在于 SubmitAjax 按钮不发送值。

为什么不起作用?

表格问题:包含备选答案和分值

qid(int), answer(varchar), point(int)

表结果:应使用 AJAX 函数发送的值

qid(int), point(int)

PHP/AJAX

<html>
<head>
<meta charset="utf-8">

<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<script type="text/javascript">
function goBack() {
window.history.go(-1);
}


$("#submitAjax").click(function(){
if($('.radiobtn').is(':checked')) {
var radiobtn = $('.radiobtn:checked').val();
var qid = $('#qid').val();

$.ajax(
{
type: "GET",
url: 'login.php',
dataType: "json",
data: "radiobtn="+radiobtn+"&qid="+qid,
success: function (response) {
if(response.status == true){
alert('points added');
}
else{
alert('points not added');
}
}
});

return false;
}
});


</script>
</head>
<body>
<?php

$localhost = "localhost";
$username = "root";
$password = "";
$connect = mysqli_connect($localhost, $username, $password) or die ("Kunde inte koppla");
mysqli_select_db($connect, 'wildfire');

if(count($_GET) > 0){
$answerPoint = intval($_GET['radiobtn']);
$qid = intval($_GET['qid']);

$sql2 = "INSERT INTO result (qid, points) VALUES ($answerPoint, $qid)";
$connect->query($sql2);
$lastid = $connect->insert_id;
if($lastid>0)
echo json_encode(array('status'=>1));
else
echo json_encode(array('status'=>0));
}

$qid = 1;
if(count($_POST) > 0){
$qid = intval($_POST['qid'])+1;

}


?>



<form method="post" action="">
<input type="hidden" name="qid" id="qid" value="<?=$qid?>">
<?php
$sql1 = mysqli_query($connect,"SELECT * FROM question where answer != '' && qid =".intval($qid));
while($row1=mysqli_fetch_assoc($sql1)){
?>
<input type='radio' name='answer1' class="radiobtn" value="<?php echo $row1['Point'];?>">
<?php echo $row1['answer'];?><br>
<?php
}
?>
<?php if ($qid <= 8) { ?>
<button type="button" onclick="history.back();">Tillbaka</button>
<button type="submit">Nästa</button>
<?php } else { ?>
<button id="submitAjax" type="submit">Avsluta provet</button>
<?php } ?>
</form>



<?php
if(count($_GET) > 0){
$answerPoint = intval($_GET['radiobtn']);
$qid = intval($_GET['qid']);

$sql2 = "INSERT INTO result (points,qid) VALUES ($answerPoint, $qid)";
$connect->query($sql2);
$lastid = $connect->insert_id;
if($lastid>0)
echo json_encode(array('status'=>1));
else
echo json_encode(array('status'=>0));
}

?>

</body>


</html>

最佳答案

$("#submitAjax").click(function(){

该行将逻辑绑定(bind)到标题中的按钮。但是,它不在 $(document).ready(function(){ ... }); 中或 $(function(){ ... });

这意味着它将在页面的其余部分加载到 DOM 之前执行,在这种情况下,它将找不到您想要绑定(bind)的按钮,并且不会执行任何操作。

您应该将逻辑包含在准备好的文档中或将其放置在正文的末尾,以便当您尝试查找它们以绑定(bind)它们时,您的元素将存在。

关于php - 使用 AJAX 将表单值提交到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34569944/

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