Javascript 如何将单选按钮的值插入数据库?
我有以下代码:Javascript 如何将单选按钮的值插入数据库?,javascript,php,jquery,html,mysql,Javascript,Php,Jquery,Html,Mysql,我有以下代码: <label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'" id = "radio">'.$answer.'</label> <button onclick="myFunction()">Value</button>'; 但只有“错误”列在递增。我尝试使用js打印所选单选按钮的值: <p id =" de
<label style="cursor:pointer;"><input type="radio" name="rads" value="'.$correct.'" id = "radio">'.$answer.'</label>
<button onclick="myFunction()">Value</button>';
但只有“错误”列在递增。我尝试使用js打印所选单选按钮的值:
<p id =" demo"></p>
<script>
function myFunction() {
var x = document.getElementById("radio").value;
document.getElementById("demo").innerHTML = x;
}
</script>
函数myFunction(){
var x=document.getElementById(“radio”).value;
document.getElementById(“demo”).innerHTML=x;
}
但当我点击按钮时,什么也没有出现。我还尝试:
var try = document.getElementsByName('rads');
for (var i = 0, len = try.length; i < length; i++) {
if (try[i].checked == 0) {
// increment wrong in db
else if (try[i].checked == 1){
//increment correct in db
}
break;
}
var try=document.getElementsByName('rads');
for(变量i=0,len=try.length;i
我真的不知道该怎么办了。如果我不清楚,很抱歉。试试这段代码
var try = document.getElementsByName('rads');
for (var i = 0, len = try.length; i < length; i++) {
if !(try[i].checked) {
// increment wrong in db
else if (try[i].checked){
//increment correct in db
}
break;
}
var try=document.getElementsByName('rads');
for(变量i=0,len=try.length;i
我无法清楚地理解您的问题,我从您的代码中发现了一个错误
<label style="cursor:pointer;">
<input type="radio" name="rads" value="<?php echo $correct; ?>" id = "radio"><?php echo $answer; ?></label>
<button onclick="myFunction()">Value</button>';
经过多次尝试和错误,下面的代码终于对我起到了作用。我添加了ajax函数,单选按钮在该函数下单击时调用
<script>
function addVote(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","try.php?rad="+str,true);
xmlhttp.send();
}
}
</script>
<?php
$q = $_GET['rad'];
$con = mysqli_connect('localhost','','','try');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con, 'try');
if($q == 1){
$sql="UPDATE count SET correct = correct+1, wrong=wrong+0 WHERE question_id = 1 ";
} else{
$sql="UPDATE count SET correct=correct+0, wrong=wrong+1 WHERE question_id = 1 ";
}
$result = mysqli_query($con,$sql);
mysqli_close($con);
?>
我认为这不起作用,因为用户必须单击按钮才能在数据库中更新值。if!(请尝试[I]。选中)
这可能不起作用。
<?php
$q = $_GET['rad'];
$con = mysqli_connect('localhost','','','try');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con, 'try');
if($q == 1){
$sql="UPDATE count SET correct = correct+1, wrong=wrong+0 WHERE question_id = 1 ";
} else{
$sql="UPDATE count SET correct=correct+0, wrong=wrong+1 WHERE question_id = 1 ";
}
$result = mysqli_query($con,$sql);
mysqli_close($con);
?>
onclick="addVote(this.value)"