Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i store select value into a php variable

Tags:

php

i have a simple drop down select menu.

<div id="select">
<select class="select">
  <option value="year 1">year 1</option>
  <option value="year 2">year 2</option>
  <option value="year 3">year 3</option>
</select>
</div>

How do i take the value that the user selects and store it into a php variable?

like image 909
user3121403 Avatar asked Jun 22 '26 20:06

user3121403


2 Answers

in somephpfile.php

$selected = $_POST['somename'];

html

 <form action="somephpfile.php" method="post"> 
    <div id="select">
    <select class="select">
    <select name="somename"> <!-- you missed this -->
      <option value="year 1">year 1</option>
      <option value="year 2">year 2</option>
      <option value="year 3">year 3</option>
    </select>
    </div>
    </form>
like image 199
user1844933 Avatar answered Jun 25 '26 11:06

user1844933


you missed the "name" attribut in the select and the form tag. Try this:

<HTML><BODY>

<?PHP
  $sel_year= $_POST['select'];

  echo $sel_year
?>


<FORM method="post" action="...your-php-file-name-here...">

<div id="select">
<select name="select">
  <option value="year 1">year 1</option>
  <option value="year 2">year 2</option>
  <option value="year 3">year 3</option>
</select>
</div>

</FORM>

</BODY></HTML>
like image 32
Thomas Avatar answered Jun 25 '26 11:06

Thomas