php jquery“变量”到php“变量”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17267468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery 'variable' to php 'variable'
提问by user2446342
I have the same problem here, but what I need is to get the value of selected option then save it in a php variable rather than a textbox. I spent almost 2days searching for my gold. Any help will do.thank you.
我在这里遇到了同样的问题,但我需要的是获取所选选项的值,然后将其保存在 php 变量而不是文本框中。我花了将近 2 天的时间寻找我的黄金。任何帮助都可以。谢谢。
Edited Here is how I write the code.
编辑这是我编写代码的方式。
<script>
$(document).ready(function () {
$("#country").change(
function () {
var options = {
url: "test.php",
type: "post",
dataType: "json",
data: "country=" + $(this).val(), //build your data string here
success: function (json) {
$("#textbox").val(json.country);
}
};
$.ajax(options);
}
);
});
</script>
<select id="country" name="country">
<option value="Delhi" >India</option>
<option value="manila" >phil</option>
<option value="tokyo" >japan</option>
</select>
<?php
@$d = $_POST['country'];
echo $d;
var_dump($d);
?>
回答by skparwal
You can do like this. (jQuery Ajax example)
你可以这样做。(jQuery Ajax 示例)
on dropdown selection:
关于下拉选择:
<select id="city">
<option value="New York">New York</option>
<option value="London">London</option>
<option value="Washington DC">Washington DC</option>
</select>
JS Code
JS代码
$(document).ready(function() {
$('city').onchange(function() {
$.ajax({
type: "GET",
url: "some.php",
data: { city: this.val() }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
});
});
PHP
PHP
some.php
一些.php
you can get the value by using GET or REQUEST methods.
您可以使用 GET 或 REQUEST 方法获取该值。
$city = $_GET['city'];
$city = $_REQUEST['city'];
回答by DevZer0
I will take the same example and modify and give you an answer.
我会以同样的例子并修改并给你一个答案。
<script>
$(document).ready(function () {
$("#country").change(
function () {
var options = {
url: "/path/to/your.php",
type: "post",
dataType: "json",
data: "country=" + $(this).val(), //build your data string here
success: function (json) {
$("#textbox").val(json.country);
}
};
$.ajax(options);
}
);
});
</script>
<select id="country" name="country">
<option value="Delhi" >India</option>
<option value="manila" >phil</option>
<option value="tokyo" >japan</option>
</select>
in order to capture the value on the server side in your php file
为了在您的 php 文件中捕获服务器端的值
$var = $_POST['country'];
$json = array('country' => $var);
header("Content-Type: application/json");
echo json_encode($json);
Updated code will return the country
back to the script via json
and write to a <input type=text
field which has id=textbox
更新后的代码将返回country
脚本通过json
并写入<input type=text
具有id=textbox