简体   繁体   中英

How to pass on change, the value of a drop down list to a PHP variable?

my question is how can I get the value of the current selected option

<select id="termid" class="selectfield">
    <option>Επιλέξτε</option>
    <option value="1">Number1</option>
    <option value="2">Number2</option>
</select>

and use it as a PHP variable in $value without submiting or updating the page ?

<?php echo get_post_meta($value, "Note1", true); ?>

I made use of this to load a file but I can not do it for the above.

$(document).ready(function() {
    $('#termid').change(function() {
        var val = $(this).val();
        $('#reservationdetails').empty().addClass('loading').load('../' + val + '.php', function(){$('#reservationdetails').removeClass('loading') });
    });
});

Thank you for any help

You can use PHP GET method to transfer the variable to PHP. For example, in your select option, use Javascript ( I mean, when user clicks on the option, this will happen: He will be redirected to yoursite.com/sub.php?value=1 ) . now on sub.php, you can use GET Variable like this: $_GET['value'] .

<?php
if($_GET['value'] == 1){ 
// do this
}
else if($_GET['value'] == 2){
// do this
}
?>

if you want to save it in a variable, then do this:

$value = $_GET['value'];

您应该获得所选选项的值:

var val = $('#termid option:selected').val();

So you want to assign a value to a PHP variable based on a choice the user makes on the client side?

You can't without submitting/updating in some way because PHP is handled server-side and once it hits the client, all PHP work is 100% done. Now you can either do something in Javascript that will perhaps trigger hidden form elements to become activated that will be useful in a triggered refresh (perhaps along with HTML that'll become visible to indicate the change) and/or assign the selected data to a JavaScript variable and do whatever needs to be done within JavaScript before ultimately sending the data back to PHP in some way.

But nothing can be done with the PHP once it leaves the server.

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