开发者

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

开发者 https://www.devze.com 2023-04-03 23:14 出处:网络
my question is how can I get the value of the current selected option <select id=\"termid\" class=\"selectfield\">

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 should get the value of the selected option:

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


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'];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消