获取< select>的值而不使用php在同一页面上提交 [英] get the value of <select> without submitting on the same page using php

查看:196
本文介绍了获取< select>的值而不使用php在同一页面上提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人可以帮助我..
i让我的程序变得更简单,让每个人都明白..
i希望我的程序在不提交的情况下获得价值,我知道这只能做到通过javascript或jquery,所以我使用onChange,但我想要的是,当我选择一个选项的值应立即传递在同一页面,但使用PHP ..

 < select id =id_selectname =nameonChange =name_click()> 
< option value =1>一个< / option>
< option value =2> two< / option>
< / select>

< script>
函数name_click(){
value_select = document.getElementById(id_select)。value;
}
< / script>

然后我应该将value_select传递给php方法。我不知道我会怎么做它..请帮助我..

解决方案

在不提交页面的情况下,您无法使用PHP进行此操作。 PHP代码在之前在服务器上执行,该页面在浏览器中呈现。当用户在页面上执行任何操作时(例如选择下拉列表中的项目),则不再有PHP。您可以通过提交该页面来获取此代码的唯一方法。



然而,您可以使用JavaScript来获取值 - 然后发送一个AJAX请求到一个PHP脚本传递选定的值,然后处理结果,例如

  $(文件).ready(函数(){
$('#my_select')。on('change',do_something);
});

函数do_something(){
var selected = $('#my_select')。val();
$ .ajax({
url:'/you/php/script.php',
类型:'POST',
dataType:'json',
data:{value:selected},
success:function(data){
$('#some_div')。html(data);
}
});





$ b

使用此代码,只要所选选项在下拉列表中发生变化,POST请求将被解雇你的PHP脚本,将选定的值传递给它。然后返回的HTML将被设置为ID some_div 的div。


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>

and then i should pass the value_select into php in post method.. i dont know how i will do it.. please help me..

解决方案

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.

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);
        }
    });
}

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在同一页面上提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆