[英]How to Set An HTML SELECT element value to php variable ONSELECT
I am Using an HTML dropdown on a page, my requirement is to when i select any value from dropdown, it want to be store in an PHP variable on the same page without submitting form or without refreshing the page. 我正在页面上使用HTML下拉列表,我的要求是当我从下拉列表中选择任何值时,它希望存储在同一页面的PHP变量中,而无需提交表单或刷新页面。
my code is: 我的代码是:
Script: 脚本:
<script type="text/javascript">
$("#bank_name").change(function(){
var data=$(this).val();
alert(data);
$.ajax({
url:"transaction.php",
type:"POST",
data: ({"bank_code": data}),
sucess: function(response){
console.log("Success");
alert(data);
},
error: function(response){
console.log("Error");
alert(data);
}
});
<script>
PHP Code: PHP代码:
$bank_code=$_POST["bank_code"];
echo "Bank Code is:",$bank_code;
it gets the value of selected option and also alerts the value but after alert it doesn't echo the value in PHP. 它获取选定选项的值,并警告该值,但警告后,它不会在PHP中回显该值。
You have type error in success
,You should remove those () brackets in data
field to access $_POST
in php like below, 您
success
输入错误,应该删除data
字段中的那些()括号,以访问php中的$_POST
,如下所示,
<script type="text/javascript">
$("#bank_name").change(function(){
var data=$(this).val();
alert(data);
$.ajax({
url:"transaction.php",
type:"POST",
data: {bank_code: data),
success: function(response){
console.log("Success");
alert(response);
},
error: function(response){
console.log("Error");
alert(data);
}
});
<script>
The echo will be received as response
in the success
callback 回声将作为
success
回调中的response
被接收
$("#bank_name").change(function() {
var data = $(this).val();
alert(data);
$.ajax({
....,
success: function(response) {
console.log("Success");
// alert response instead
alert(response);
},
......
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.