[英]not able to pass value using ajax in php file
I am not able to pass value using ajax in php file.我无法在 php 文件中使用 ajax 传递值。
Corrected Code更正代码
<script>
$("body").on('change', '#area', function () {
//get the selected value
var selectedValue = $(this).val();
//make the ajax call
$.ajax({
url: 'box.php',
type: 'POST',
data: {option: selectedValue},
success: function () {
console.log("Data sent!");
}
});
});
</script>
here the php code这里是php代码
<?php $val=$_POST['option'];echo $val; ?>
There are a few problems here:这里有几个问题:
It should be url
, not rl
.它应该是
url
,而不是rl
。 Also, you have type: POST'
with it ending in a '
, but no starting '
.此外,您
type: POST'
,它以'
结尾,但没有开头'
。
It should be type: 'POST'
.它应该是
type: 'POST'
。
It should then look like this:它应该看起来像这样:
$("body").on('change', '#area', function() {
var selectedValue = this.value;
$.ajax({
url: 'box.php',
type: 'POST',
data: {
option : selectedValue
},
success: function() {
console.log("Data sent!");
}
});
});
If you want to view your data on the same page after (as on box.php
, you are echo
'ing the value.), you can do this:如果你想在同一页面上查看你的数据(就像在
box.php
上box.php
,你正在echo
显值。),你可以这样做:
success: function(data) {
console.log(data);
}
This will then write in the console what option
is, which is the value of #area
.这将在控制台中写入什么
option
,即#area
的值。
Try the following code试试下面的代码
$("body").on('change',function(){
$.ajax({
URL:<you absolute url>,
TYPE:POST,
Data:"variable="+$("#area").val(),
Success:function(msg){
<do something>
}
});
});
Hope this will help you in solving your problem.希望这会帮助您解决您的问题。
Your just miss ajax method parameter spelling of 'url' and single quote before value of type ie 'POST'.您只是错过了 'url' 的 ajax 方法参数拼写和类型值之前的单引号,即 'POST'。 It should be like
它应该像
$.ajax({
url: 'box.php',
type: 'POST',
data: {option : selectedValue},
success: function() { console.log("Data sent!");}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.