根据javascript值更改php变量

发布于 2024-11-30 06:59:33 字数 758 浏览 7 评论 0原文

我如何根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

溺深海 2024-12-07 06:59:33

您想要的执行不会发生,因为 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:

enter image description here

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)"

戏剧牡丹亭 2024-12-07 06:59:33

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.

罪歌 2024-12-07 06:59:33

这正是我们使用ajax的原因。
使页面以默认首选项加载,然后使用 ajax 更新它

that's exactly we use ajax.
make the page loads in default preferences, then update it using ajax

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文