admin管理员组

文章数量:1401629

how can i change/assign a value to php variable depending on a javascript variable? here is my code.

<select id="reOute" name="reOute" onchange="OnChange(this.form.reOute);">
<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.

how can i change/assign a value to php variable depending on a javascript variable? here is my code.

<select id="reOute" name="reOute" onchange="OnChange(this.form.reOute);">
<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.

Share Improve this question asked Aug 17, 2011 at 23:10 PhphelpPhphelp 1,3302 gold badges14 silver badges25 bronze badges 3
  • php is executed server-side (so before the page is loaded) and javascript is executed client-side (so dynamically on the users end). So you can only pass a variable from php to javascript. Not vice-versa. At least directly. You would need to resend the data to the server. You could consider ajax to do something similar? – Nathaniel Wendt Commented Aug 17, 2011 at 23:12
  • oh.. i don't know ajax.. :( is there some other way to change the php variable depending on the dropdown box? i mean something other than onChange? – Phphelp Commented Aug 17, 2011 at 23:16
  • Im afraid not. Based on the nature of when the code runs, its just not doable without ajax. You can always change the value that gets sent through php after the dropdown box. But again, it requires a page request (page load) to do this. – Nathaniel Wendt Commented Aug 17, 2011 at 23:24
Add a ment  | 

3 Answers 3

Reset to default 4

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 puter 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 puter.

Actually, your question seems to be closer to a "what is the best way to do (something)"

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.

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

本文标签: Change php variable based on javascript valueStack Overflow