2

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.

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? 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? 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. Commented Aug 17, 2011 at 23:24

3 Answers 3

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:

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

if nothing else comes up, i will try doing that.. unfortunately there is a lot of complications (for me) behind that.. :(
0

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

2 Comments

i have never used ajax.. :( can you please help?
"AJAX" is the use of XmlHTTPRequest to call request server pages with Javascript without reloading the page. That's how live webpages (non-flash) update content. In your case you could send the values you need to a separate php script and have it execute something on the server for you. It's hard to say without more details about what you're trying to do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.