Change php variable based on javascript value

早过忘川 提交于 2019-12-24 01:22:03

问题


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.


回答1:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/7100633/change-php-variable-based-on-javascript-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!