问题描述
希望有人可以帮助我..我使我的程序更简单,以便每个人都可以理解..我希望我的程序在不提交的情况下获取的值,我知道这只能通过javascript或jquery完成,所以我使用onChange,但是我想要的是当我选择一个选项时,该值应立即在同一位置传递页面,但使用php ..
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..
<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>
然后我应该在post方法中将value_select传递到php中.我不知道我该怎么做..请帮助我.
and then i should pass the value_select into php in post method.. i dont know how i will do it.. please help me..
推荐答案
在未提交页面的情况下,无法使用PHP进行此操作.在页面 之前,PHP代码在服务器上执行,然后在浏览器中呈现页面.然后,当用户在页面上执行任何操作(例如,在下拉列表中选择一个项目)时,就不再有PHP.将此代码导入PHP的唯一方法是提交页面.
You cannot do this using PHP without submitting the page. PHP code executes on the server before the 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.
您可以要做的是使用javascript获取值-然后向传递所选值的php脚本触发AJAX请求,然后处理结果,例如
What you can do 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.
$(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);
}
});
}
使用此代码,每当下拉菜单中的选定选项发生变化时,都会向您的php脚本触发POST请求,并将选定的值传递给它.然后,返回的HTML将被设置为ID为some_div
的div.
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
.
这篇关于获得& lt; select& gt;的值无需使用php在同一页面上提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!