根据javascript值更改php变量
我如何根据 javascript 变量更改/分配 php 变量的值? 这是我的代码。
<select id="reOutcome" name="reOutcome" onchange="OnChange(this.form.reOutcome);">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
<script type="text/javascript">
function OnChange(dropdown)
{
var myindex = dropdown.selectedIndex
var SelValue = dropdown.options[myindex].value
if(SelValue==2){
<?php
$sCOMPFields .= "|"."SreComments";
$sCOMPFields .= "|"."rePrID";
?>
}
return true;
}
</script>
onchange 函数工作正常。我只是不知道如何更改 php 变量。我在网上搜索了很多。我所得到的只是如何将 php 变量分配给 javascript。那不是我要找的。 感谢您的帮助。
how can i change/assign a value to php variable depending on a javascript variable?
here is my code.
<select id="reOutcome" name="reOutcome" onchange="OnChange(this.form.reOutcome);">
<option value="1">Value 1</option>
<option value="2">Value 2</option>
<option value="3">Value 3</option>
</select>
<script type="text/javascript">
function OnChange(dropdown)
{
var myindex = dropdown.selectedIndex
var SelValue = dropdown.options[myindex].value
if(SelValue==2){
<?php
$sCOMPFields .= "|"."SreComments";
$sCOMPFields .= "|"."rePrID";
?>
}
return true;
}
</script>
The onchange function is working fine. I just don't know how to change the php variable. I searched online a lot. All im getting is how to assign php variable to javascript. That is not what im looking for.
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要的执行不会发生,因为 php 作用域和 javascript 作用域的流程发生在不同的时刻。它是这样的:
因此,当 javascript 正在执行时,您无法执行 php通过浏览器在用户的计算机上执行,但是您可以在您的服务器上执行php来生成您需要在用户计算机上执行的javascript。
实际上,你的问题似乎更接近于“做(某事)的最佳方法是什么”
The execution you want won`t occur, because the flow of the php scope and the javascript scope occurs on different moments. It is something like this:
So, you can`t execute php while the javascript is being executed on the computer of the user through the browser, but you can execute php on your server to generate the javascript you need to be executed on the user computer.
Actually, your question seems to be closer to a "what is the best way to do (something)"
PHP 变量在运行时设置。您不能直接在 javascript 中执行此操作。我能想到的最好方法是将 php 变量设置为会话变量。然后使用 JavaScript 通过 ajax/jquery 调用可以更新会话变量的 php 文件。
PHP variables are set at run time. You can't do it directly within the javascript. Off the top of my head the best way I can think of to do it would be to set the php variables as session variables. Then use your javascript to call a php file via ajax/jquery that can update the session variables.
这正是我们使用ajax的原因。
使页面以默认首选项加载,然后使用 ajax 更新它
that's exactly we use ajax.
make the page loads in default preferences, then update it using ajax