javascript 获取 <select> 的值而不使用 php 在同一页面上提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18610023/
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
get the value of <select> without submitting on the same page using php
提问by Krishna
Hope someone can help me.. i made my program more simpler so that everybody will understand.. i want my program to get the value of the without submitting, i know that this can only be done by javascript or jquery so I use the onChange, but what I want is when i select an option the value should be passed immediately on the same page but using php..
希望有人能帮助我..我让我的程序更简单,这样每个人都会理解..我希望我的程序在不提交的情况下获得价值,我知道这只能通过javascript或jquery完成,所以我使用onChange ,但我想要的是当我选择一个选项时,该值应该立即在同一页面上传递,但使用 php..
<select id="id_select" name="name" onChange="name_click()">
<option value="1">one</option>
<option value="2">two</option>
</select>
<script>
function name_click(){
value_select = document.getElementById("id_select").value;
}
</script>
and then i should pass the value_select into php in post method.. i dont know how i will do it.. please help me..
然后我应该在 post 方法中将 value_select 传递到 php.. 我不知道我会怎么做.. 请帮助我..
回答by Aleks G
You cannot do this using PHP without submitting the page. PHP code executes on the server beforethe page is rendered in the browser. When a user then performs any action on the page (e.g. selects an item in a dropdown list), there is no PHP any more. The only way you can get this code into PHP is by submitting the page.
如果不提交页面,则无法使用 PHP 执行此操作。PHP 代码在页面在浏览器中呈现之前在服务器上执行。当用户在页面上执行任何操作(例如选择下拉列表中的项目)时,就不再有 PHP 了。将此代码导入 PHP 的唯一方法是提交页面。
What you cando however is use javascript to get the value - and then fire off an AJAX request to a php script passing the selected value and then deal with the results, e.g.
但是,您可以做的是使用 javascript 获取值 - 然后向传递所选值的 php 脚本发出 AJAX 请求,然后处理结果,例如
$(document).ready(function() {
$('#my_select').on('change', do_something);
});
function do_something() {
var selected = $('#my_select').val();
$.ajax({
url: '/you/php/script.php',
type: 'POST',
dataType: 'json',
data: { value: selected },
success: function(data) {
$('#some_div').html(data);
}
});
}
With this code, whenever the selected option changes in the dropdown, a POST request will be fired off to your php script, passing the selected value to it. Then the returned HTML will be set into the div with ID some_div
.
使用此代码,只要下拉列表中的选定选项发生更改,就会向您的 php 脚本发出 POST 请求,并将选定的值传递给它。然后返回的 HTML 将被设置到 ID 的 div 中some_div
。
回答by Senthil
My suggestion go with jquery. Try with this
我的建议是使用 jquery。试试这个
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
<script>
$(document).ready(function(){
$("#id_select").change(function(){
var url = 'http:\localhost\getdata.php'; //URL where you want to send data
var postData = {'value' : $(this).value};
$.ajax({
type: 'POST',
url: url,
data : postData,
dataType: 'json',
success: function(data) {
//
},
error: function(e) {
console.log(e.message);
}
});
})
})
</script>
In getdata.php
在 getdata.php 中
<?php
var $value = $_POST['value'];
// you can do your logic
?>
回答by Lajos Arpad
Post with ajax as Alex G was telling you (+1) and then handle the post with PHP. You can define a callback in Javascript which will run when the page responds.
使用 ajax 发布,正如 Alex G 告诉您的那样 (+1),然后使用 PHP 处理发布。您可以在 Javascript 中定义一个回调,该回调将在页面响应时运行。
回答by bipen
not sure ..but i guess ajax is what you need..
不确定..但我想 ajax 是你需要的..
<script>
function name_click(){
value_select = $("#id_select").val();
$.post('path/to/your/page',{"value":value_select},function(data){
alert('done')
})
}
</script>
PHP
PHP
$value=$_POST['value']; //gives you the value_select data