简体   繁体   English

得到...的价值 <select>无需使用php在同一页面上提交

[英]get the value of <select> without submitting on the same page using 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.. 希望有人可以帮助我..我使我的程序更简单,以便每个人都可以理解..我希望我的程序在不提交的情况下获得的价值,我知道这只能通过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中..我不知道我该怎么做..请帮助我..

You cannot do this using PHP without submitting the page. 如果不提交页面,则无法使用PHP进行此操作。 PHP code executes on the server before the page is rendered in the browser. 在浏览器中呈现页面之前 ,PHP代码在服务器上执行。 When a user then performs any action on the page (eg selects an item in a dropdown list), there is no PHP any more. 然后,当用户在页面上执行任何操作(例如,在下拉列表中选择一个项目)时,就不再有PHP。 The only way you can get this code into PHP is by submitting the page. 将此代码导入PHP的唯一方法是提交页面。

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, eg 但是,您可以使用javascript获取值-然后将AJAX请求发送到传递所选值的php脚本,然后处理结果,例如

$(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. 使用此代码,每当下拉菜单中的所选选项发生变化时,都会向您的php脚本触发POST请求,并将所选值传递给它。 Then the returned HTML will be set into the div with ID some_div . 然后,返回的HTML将被设置为ID为some_div的div。

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

My suggestion go with jquery. 我的建议与jquery一起使用。 Try with this 试试这个

<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

?>

Post with ajax as Alex G was telling you (+1) and then handle the post with PHP. Alex G告诉您(+1),然后用ajax发布信息,然后使用PHP处理该信息。 You can define a callback in Javascript which will run when the page responds. 您可以在Javascript中定义一个回调,该回调将在页面响应时运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM