如何將來自select選項的javascript變量傳遞給PHP變量? 我想根據用戶選擇設置PHP變量。
我試過代碼:將javascript變量賦值給php變量
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(function(){
$("select[name='sex']").change(function() {
var submitSearchData = jQuery('#extended-search').serialize();
var selectedValue=$('#sex').val();
jQuery.ajax({
type: "POST",
data: 'selected=' + selectedValue
url: "ajax.php",
success: function() {
// alert(submitSearchData);
alert(selectedValue);
}
});
});
});
</script>
<form id="extended-search" >
<div class="input-container">
<select class="select" name="sex" id="sex">
<option value="0">All</option>
<option value="1">M</option>
<option value="2">F</option>
</select>
</div>
</form>
<?php
var_dump ($_REQUEST['selected']); //that print NULL don't know why!
?>
問題是什麼?是否有錯誤? –
當我嘗試使用var_dump($ _REQUEST ['selected'])訪問所選變量時;它返回null – user3114378
您在「selectedValue」之後錯過了「,」嗎? – mrmoment