你能否显示你的html代码,以及你如何激发你的click_event?
阿贾克斯与 - jQuery的:
<!-- in your head-section include jQuery -->
<head>
<script src ="//code.jquery.com/jquery-1.10.2.min.js"> </script>
</head>
<!-- your forms -->
<form>
<input type="radio" name="radio1" value="foo"> Foo<br>
</form>
<form>
<input type="radio" name="radio2" value="bar"> Bar<br>
</form>
<form>
<input type="radio" name="radio3" value="foobar"> Foobar
</form>
<script >
//wait until document is ready
$(function() {
//when these are the only radio-buttons on page you can select all and bind an click-event to it,
//else add a class to all radio-buttons and select like: $(".your_class")
$(":radio").click(function() {
//get the url from the action-attribute of the form
var value = $(this).val(),
radioName = $(this).attr("name");
$.ajax({
type: "POST",
url: "/your_file.php",//the URL to your php-File
data: { name: radioName, data: value}
})
.done(function(msg) {
alert("Data Saved: " + msg);
});
});
});
</script>
在你的PHP文件,你可以现在$ _ POST [ '名']和$ _ POST通常保存POST [ '数据'。 您在此文件中输出的所有内容都将成为警报中的“msg”输出(在ajax请求中)。 你可以像也输出的JSON字符串:
echo json_encode(array("error" => TRUE, "text" => "Data could not be saved!"));
然后在你完成功能做:
if(msg.error)
alert("ERROR: " + msg.text);
else
alert("Your data has been saved :)");