how to use Ajax in this code?
</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?
Firstly, I strongly suggest you use a JS Framework like jQuery to do it. This is how you would do this in 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.
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[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).attr('value')
I would recommend using jQuery also
精彩评论