Phphelp
Phphelp

Reputation: 1330

Change php variable based on javascript value

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.

Upvotes: 2

Views: 10178

Answers (3)

bpaulon
bpaulon

Reputation: 356

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

Upvotes: 4

Jacer Omri
Jacer Omri

Reputation: 1773

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

Upvotes: 0

LoneWolfPR
LoneWolfPR

Reputation: 4100

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.

Upvotes: 0

Related Questions