</tr>
<tr>
<td><input type="radio" name="ans" id='ans_2' value="2" /> <? echo " "; echo $ans2 = $ques['ans_2'];?></td>
</tr>
<tr></tr>
<tr>
<td><input type="radio" name="ans" id='ans_3' value="3" /> <? echo " "; echo $ans3 = $ques['ans_3'];?></td>
</tr>
<tr></tr>
<tr>
<td><input type="radio" name="ans" id='ans_4' value="4" /> <? echo " "; echo $ans4 = $ques['ans_4'];?></td>
</tr>
hello everyone i am creating online MSQ's website and i need your help in this code when user click on the radio button the given answer will store in db how can i do this?
大家好,我正在创建在线MSQ的网站,当用户点击单选按钮时,我需要你的帮助,给定的答案将存储在db我怎么能这样做?
2 个解决方案
#1
1
Firstly, I strongly suggest you use a JS Framework like jQuery to do it. This is how you would do this in jQuery:
首先,我强烈建议您使用像jQuery这样的JS框架来完成它。这是你在jQuery中这样做的方法:
$('input.radio').click(function() {
$.ajax({
url: 'script.php?answer=' + $(this).attr('value'),
success: function(data) {
// this is the server response
}
});
});
Of course your script.php file should manage the DB connection and everything else. You may need to add extra parameters but this is a general solution that you can use in cases similar to this one.
当然,您的script.php文件应该管理数据库连接和其他所有内容。您可能需要添加额外的参数,但这是一个通用的解决方案,您可以在类似于此的情况下使用。
EDIT
编辑
I used the 'input.radio' selector since you didn't add any specific class to the radio buttons, of course this will apply that click function to every radio button on the page. You may need to add a more specific selector (like a class selector) to affect only the correct buttons
我使用了'input.radio'选择器,因为你没有在单选按钮上添加任何特定的类,当然这会将click功能应用到页面上的每个单选按钮。您可能需要添加更具体的选择器(如类选择器)以仅影响正确的按钮
#2
0
$('input.radio[name='ans']).click(function() {
$.ajax({
url: 'script.php?answer=' + this.value,
success: function(data) {
// this is the server response
}
});
});
});
Pretty much the same as above. Except you can spercify this group of inputs by name:
和上面几乎一样。除非您可以按名称对这组输入进行分类:
$('input.radio[name='ans']).click // binds the ajax function to the click event
And you can just use this.value
instead of:
你可以使用this.value而不是:
$(this).attr('value')
I would recommend using jQuery also
我也建议使用jQuery
#1
1
Firstly, I strongly suggest you use a JS Framework like jQuery to do it. This is how you would do this in jQuery:
首先,我强烈建议您使用像jQuery这样的JS框架来完成它。这是你在jQuery中这样做的方法:
$('input.radio').click(function() {
$.ajax({
url: 'script.php?answer=' + $(this).attr('value'),
success: function(data) {
// this is the server response
}
});
});
Of course your script.php file should manage the DB connection and everything else. You may need to add extra parameters but this is a general solution that you can use in cases similar to this one.
当然,您的script.php文件应该管理数据库连接和其他所有内容。您可能需要添加额外的参数,但这是一个通用的解决方案,您可以在类似于此的情况下使用。
EDIT
编辑
I used the 'input.radio' selector since you didn't add any specific class to the radio buttons, of course this will apply that click function to every radio button on the page. You may need to add a more specific selector (like a class selector) to affect only the correct buttons
我使用了'input.radio'选择器,因为你没有在单选按钮上添加任何特定的类,当然这会将click功能应用到页面上的每个单选按钮。您可能需要添加更具体的选择器(如类选择器)以仅影响正确的按钮
#2
0
$('input.radio[name='ans']).click(function() {
$.ajax({
url: 'script.php?answer=' + this.value,
success: function(data) {
// this is the server response
}
});
});
});
Pretty much the same as above. Except you can spercify this group of inputs by name:
和上面几乎一样。除非您可以按名称对这组输入进行分类:
$('input.radio[name='ans']).click // binds the ajax function to the click event
And you can just use this.value
instead of:
你可以使用this.value而不是:
$(this).attr('value')
I would recommend using jQuery also
我也建议使用jQuery