0

I have a drop down box which has 150 options I want to get the value of current option selected.I tried to achieve that using javascript and was successful.Now what I want is I want to pass the value to php so that php knows which option is selected and on that basis I have to change the value of a particular variable in PHP. How can I achieve this type of functionality.This is my code to select which option is selected in drop down menu in javascrpit

    function myfun(){
    var e=document.getElementByName("location");
    var strUser = e.options[e.selectedIndex].value;

strUser will ocntain the index value of dropdown option But how can I use this value in php

This is my drop down code

<form action="final.php" method="post">
<select style="width: 200px;" name="location" onchange="myfun">
  <option value="All">All</option>
  <option value="Noida Sector 1">Noida Sector 1</option>
  <option value="Noida Sector 2">Noida Sector 2</option>
  <option value="Noida Sector 3">Noida Sector 3</option>
  <option value="Noida Sector 4">Noida Sector 4</option>
  <option value="Noida Sector 5">Noida Sector 5</option>
  <option value="Noida Sector 6">Noida Sector 6</option>
  <option value="Noida Sector 7">Noida Sector 7</option>
  </select>
2
  • you have any php code? Commented Jun 25, 2015 at 11:19
  • some of your php code would help Commented Jun 25, 2015 at 11:22

2 Answers 2

2

Once the form has been submitted, you will be able to access the value of what has been selected in php by simply accessing $_POST['location']; this will contain the contents of the value attribute of the submitted option.

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

2 Comments

No I dont want to submit the form but want to get value of dropdown variable as soon as option changes without submitting
In which case, you'll need to pass the value to php via ajax, and POST it that way.
0

You can add a hidden filed which you kept empty, but as soon as any one selected a particular option then assign the value of those option to those hidden field and then you can use this hidden field value after form post which you will get in

$_POST['hiddenSelectedOption']

Comments

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.