Prabhjot Singh Kainth
Prabhjot Singh Kainth

Reputation: 1861

dropdown value pass from javascript to php

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>

Upvotes: 0

Views: 65

Answers (2)

sanjay singh
sanjay singh

Reputation: 33

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']

Upvotes: 0

d0ug7a5
d0ug7a5

Reputation: 712

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.

Upvotes: 2

Related Questions