[英]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 ? 并将其用作
$value
的PHP变量, 而无需提交或更新页面 ?
<?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. 您可以使用PHP GET方法将变量传输到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 ) .
例如,在您的选择选项中,使用Javascript(我的意思是,当用户单击该选项时,会发生这种情况:他将被重定向到yoursite.com/sub.php?value=1)。 now on sub.php, you can use GET Variable like this: $_GET['value'] .
现在在sub.php上,您可以像这样使用GET变量:$ _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? 因此,您要根据用户在客户端做出的选择为PHP变量分配值?
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. 您不能以某种方式提交/更新,因为PHP是在服务器端处理的,一旦它到达客户端,所有PHP工作都会100%完成。 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.
现在,您可以用Javascript做一些可能会触发隐藏的表单元素以使其被激活的操作,这将在触发刷新中很有用(也许与可见的HTML一起指示更改)和/或将选定的数据分配给JavaScript变量,然后在以某种方式最终将数据发送回PHP之前执行JavaScript中需要做的所有事情。
But nothing can be done with the PHP once it leaves the server. 但是,一旦PHP离开服务器,就无法对其进行任何处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.