我一直在尝试发送用户选择的单选按钮的值并使用php恢复该值,但问题是我无法恢复该值。这是我的代码:
<form name="submission" action="">
<input type="radio" name="ex1" id="ex1_a" value="1">
<input type="radio" name="ex1" id="ex1_b" value="2">
<input type="radio" name="ex1" id="ex1_c" value="3">
<button class="buttonS" type="submit">
Submit
</button>
</form>
$(function() {
$(".buttonS").click(function() {
// validate and process form here
var radio_button_value;
if ($("input[name='ex1']:checked").length > 0){
radio_button_value = $('input:radio[name=ex1]:checked').val();
}
else{
alert("No button selected, try again!");
return false;
}
$.ajax({
type: "POST",
url: "save.php",
data: radio_button_value,
success: function() {
alert("form submitted: "+ radio_button_value);
}
});
return false;
});
});
<?php
$selected_button = $_POST['ex1'];
echo "Test";
echo $selected_button;
?>
自显示警报以来它似乎工作的AJAX部分,但我不知道它是否正确发送值或者如果php错误,则显示echo“Test”,但echo $ selected_button从不出现。我将不胜感激任何帮助。
答案 0 :(得分:4)
在ajax函数中,您必须指定参数名称以及值:
$.ajax({
type: "POST",
url: "save.php",
data: {"ex1":radio_button_value},
success: function() {
alert("form submitted: "+ radio_button_value);
}
});