user2379090
user2379090

Reputation: 123

How to link HTML drop down list

I am basically trying to write a PHP script which will display a message when a certain Option/value is selected in the drop down list.

Here is the html for some clarification

 <select name="month">
 <option value="0">0</option>
<option value="1">January</option>
<option value="2">Febuary</option>
<option value="3">March</option>
<option value="4">April</option>
</select>

Basically, I want the PHP script to print a statement when an option, say the first option (January), is selected.

Upvotes: 0

Views: 2736

Answers (4)

Carl Vitullo
Carl Vitullo

Reputation: 923

That's not something that can be done with PHP. PHP is executed server-side, meaning it delivers a static page once all the server-side processing has been completed. To change something, it would have to be submitted back to the server and redelivered.

Javascript is what you'll want to use here. Check out the Codecademy jQuery track, it's really helpful for figuring out how to do client-side work the way you want. http://www.codecademy.com/tracks/jquery

Basically you'd want to have an onchange="function()" attribute for the select, which would call a javascript function to display a message in a separate area.

For example (not quite what you want but illustrates the principle:

<script type="text/javascript">
    var changed = function() {
        alert("Value of select is now: " + event.target.value);
    };
</script>

<form action="index.php" method="POST"> 
  <select name="month" onchange="changed()">
    <option value="0">0</option>
    <option value="1">January</option>
    <option value="2">Febuary</option>
    <option value="3">March</option>
    <option value="4">April</option>
  </select>
</form>

Upvotes: 0

Amal
Amal

Reputation: 76676

It's probably done better on client-side but since you wanted to do it with PHP, here's a simple solution using associative arrays:

somefile.php:

<?php 
$months = array(1 => 'January', 2 => 'February', 3 => 'March', 4 => 'April', 5 => 'May',
    6 => 'June', 7 => 'July', 8 => 'August', 9 => 'September', 10 => 'October', 11 => 'November',
    12 => 'December');

if(isset($_POST['sb'])){

$input = $_POST['month'];
echo "You selected ".$months[$input];

}
?>
<form action="" method="post">
<select name="month">
<option value="1">January</option>
<option value="2">Febuary</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>

</select>
<input type="submit" name="sb"/>
</form>

And if you want to generate your menu dynamically, you could do something like this:

<?php
$curr_month = date("m");
$month = array (1=>"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
$select = "<select name=\"month\">\n";
foreach ($month as $key => $val) {
    $select .= "\t<option val=\"".$key."\"";
    if ($key == $curr_month) {
        $select .= " selected=\"selected\">".$val."</option>\n";
    } else {
        $select .= ">".$val."</option>\n";
    }
}
$select .= "</select>";
echo $select;
?>

Hope this helps!

Upvotes: 1

Jake N
Jake N

Reputation: 10583

If you want the action to take place when the select is changed you need a client side solution like javascript, preferably jQuery.

PHP cannot do what you asking, it can do something when the form is submitted but not when the select is changed.

http://api.jquery.com/change/

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>

$(document).ready(function(){
    $("select").on("change", function(){
         alert("The value is " + $(this).val());
    });
});

That will produce an alert when your select is changed.

Upvotes: 2

luttkens
luttkens

Reputation: 1264

If your html looks like this:

<form action="index.php" method="POST"> 
  <select name="month">
    <option value="0">0</option>
    <option value="1">January</option>
    <option value="2">Febuary</option>
    <option value="3">March</option>
    <option value="4">April</option>
  </select>
  <input type="submit" value="Select">
</form>

Then in your index.php:

echo "You selected " . $_POST['month'];

Upvotes: -1

Related Questions