DZvonko Nikolov
DZvonko Nikolov

Reputation: 21

dropdown return value in html and php

I have dropdown list (menu) and a button created with this code:

 <form name="period" action="all.php" method="POST">
 <div align="center">
 <select name=period_dropdown>
 <option value="nill"></option>
 <option value="48">48</option>
 <option value="72">72</option>
 <option value="96">96</option>
 <option value="120">120</option>
 </select>

 <input type="submit" value= "OK" >
 </div></form>

When option of dropdown is selected, must be on button instead of OK and when button is pressed to be assigned to variable $period1. So, when I select 72, button must have 72 instead of OK and when I click button, variable $period1 to get value 72 (integer). Please, no javascript. Just html and php.

Thanks

Upvotes: 1

Views: 3325

Answers (3)

HyderA
HyderA

Reputation: 21391

<form name="period" action="all.php" method="POST">
<div align="center">
<select id="period_dropdown" name="period_dropdown" onchange="updateButton()">
<option value="nill"></option>
<option value="48">48</option>
<option value="72">72</option>
<option value="96">96</option>
<option value="120">120</option>
</select>

<input id="period_button" type="submit" value= "OK" >
</div></form>

<script type="text/javascript">
function updateButton() {
    document.getElementById("period_button").value = document.getElementById("period_dropdown").value;
}
</script>

Upvotes: 0

Chris McKinnel
Chris McKinnel

Reputation: 15102

In all.php:

  if (isset($_POST['period_dropdown'])) {
    $period1 = $_POST['period_dropdown'];

    //do something with $period1
  }

?>

Upvotes: 0

Paul S.
Paul S.

Reputation: 1537

You have to use javascript for what you are trying to do. If you want the button to change on the fly without submitting the form, you have to do client-side scripting (javascript).

Either:

  1. Change the value of button after the dropdown is selected and form is submitted

  2. Use two lines of javascript to change the value of the button when the select box is changed

Upvotes: 2

Related Questions