gpt4 book ai didi

javascript - 使用ajax执行php插入功能

转载 作者:行者123 更新时间:2023-11-27 23:14:52 27 4
gpt4 key购买 nike

我有一个 html<input type='radio'> 形式

<form action="results.php" method="post" enctype="multipart/form-data" onsubmit='return false'><br>
<p>Have you ever turned a client down?</p>
<div id="q_1">
<input type="radio" name="q1" id="q_1_yes" value="yes">
<label for="q_1_yes">Yes</label>
<input type="radio" name="q1" id="q_1_no" value="no">
<label for="q_1_no">No</label>
</div><br>
<p>Are you comfortable with failure?</p>
<div id="q_1">
<input type="radio" name="q2" id="q_2_yes" value="yes">
<label for="q_2_yes">Yes</label>
<input type="radio" name="q2" id="q_2_no" value="no">
<label for="q_2_no">No</label>
</div><br>
<input type="submit" onclick='return handleClick();' name="sub_eit" id="sub_eit" value="Submit">
</form>

我有一个 javascript function检查值为"is"的单选按钮的数量是否大于值为“否”的数量,如下所示

function handleClick()
{
var amountYes = 0;
for(var i = 1; i <= 4; i++) {
var radios = document.getElementsByName('q'+i);
for(var j = 0; j < radios.length; j++) {
var radio = radios[j];
if(radio.value == "yes" && radio.checked) {
amountYes++;
}
}
}
//code to perform php insert function if yes is less than or equal to 2
if (amountYes <= 2) {
$.ajax({
type: "POST",
url: "results.php",
dataType: "json",
success: function (response) {
}
});
} else {
alert("Correct Responses: " + amountYes);
}
}

results.php

if (isset($_POST['q_1']) && isset($_POST['q_2']))
{
$q_1 = $_POST['q_1'];
$q_2 = $_POST['q_2'];

$yes = "yes";
$no = "no";

$id = $_SESSION['id'];
$u_name = $_SESSION['uname'];

$recommend = "Executive";

$osql =<<< EOF
INSERT INTO results (id, recomend, u_name, status) VALUES ('$id', '$recommend', '$u_name', '1');
EOF;
$ret = $db->exec($osql);
}

但是 ajax代码似乎不起作用。请问是什么问题。在此先感谢您的帮助。非常感谢

最佳答案

您已在 ajax 请求中包含发布数据。

 $.ajax({
type: 'POST',
url: 'results.php',
data: $('form').serialize(),
dataType: 'json',
success: function( response){
console.log( 'the feedback from your result.php: ' + response);
}
});

并且在您的 result.php 中,您必须将 $_POST['q_1'] 重命名为 _POST['q1'] 等等。 q1 和 q2 是您输入 radio 的名称。

关于javascript - 使用ajax执行php插入功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43680511/

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