简体   繁体   中英

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..

<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 (eg 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, eg

$(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 .

not sure ..but i guess ajax is what you need..

<script>
function name_click(){
  value_select = $("#id_select").val();
  $.post('path/to/your/page',{"value":value_select},function(data){
     alert('done')
 })

}
</script>

PHP

$value=$_POST['value']; //gives you the value_select data

My suggestion go with 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

<?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. You can define a callback in Javascript which will run when the page responds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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