我正在尝试使用 Javascript/AJAX 将选定单选按钮的值放入 PHP 会话变量中。一切正常,除了页面刷新。有什么办法不刷新页面吗?
的HTML:
<input type="radio" name="shipping_method" value="Standard"> Standard<br>
<input type="radio" name="shipping_method" value="2-day"> 2-Day Air*<br>
<input type="radio" name="shipping_method" value="Overnight"> Overnight<br>
Javascript/AJAX(注释掉了一些警报):
<script>
$(document).ready(function() {
$("input[name=shipping_method]").on('change', function(){
var shipping_method = $('input:radio[name=shipping_method]:checked').val();
/* alert (shipping_method); */
$.ajax ({
type: "GET",
url: ajax/shipping_method.php?shipping_method="+shipping_method,
success : function(data){
/* alert(data); */
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("problem: " + errorThrown);
}
});
});
});
shipping_method.php:
<?php session_start(); ?>
<?php
$shipping_method = $_GET['shipping_method'];
$_SESSION['shipping_method'] = $shipping_method;
?>