Adding additional functionality to a PHP form
Given the code below when I select a value from my dropdown box [S, M, L] and hit submit I get one of the following outputs:
S is equal to
M is equal to
L is equal to
I would like the output to be along the lines of
S is equal to Small
M is equal to Medium
L is equal to Large
Can something be added to my code to accomplish this? Or do I need to take a different approach?
<form action="?" method="post">
<?php
$size = array();
$size[] = "";
$size[] = "S";
$size[] = "M";
$size[] = "L";
if(isset($_REQUEST['list'])){
echo $size[(int)$_REQUEST['list']]." is equal to "."<br />";
}
echo '<s开发者_StackOverflowelect name="list">'."\n";
$count = 0;
foreach($size as $size){
echo '<option value="'.$count.'">'.$size.'</option>'."\n";
$count++;
}
echo '</select>';
?>
<input type="submit" value="submit" />
</form>
<form action="?" method="post">
Why not use an associative array and then you don't have to mess around with ints or $count
s?
<form action="?" method="post">
<?php
$sizes = array(
"S" => "Small",
"M" => "Medium",
"L" => "Large"
);
if(isset($_REQUEST['list'])){
echo "<p>" . $_REQUEST['list'] . " is equal to " . $sizes[$_REQUEST['list']] . "</p>";
}
?>
<select name="list">
<option value="">Choose one</option>
<?php
foreach($sizes as $key => $val){
echo "<option value='$key'>$key - $val</option>\n";
}
?>
</select>
<input type="submit" value="submit" />
</form>
The output will look something like this:
S is equal to Small
+------------+-+
| Choose one |▼|
+------------+-+
| S - Small |
| M - Medium |
| L - Large |
+--------------+
With this solution you can reuse the original static array to populate the post echo. Also try to avoid using \n in your html instead use the semantic <br>
.
<form action="?" method="post">
<?php
$size = array(""=>"","Small"=>"S","Medium"=>"M","Large"=>"L");
if(isset($_REQUEST['list'])){
echo $size[$_REQUEST['list']]." is equal to ".$_REQUEST['list']."<br />";
}
echo "<select name='list'>";
foreach($size as $key=>$value){
echo "<option value='$key'>$value</option>";
}
echo '</select>';
?>
<input type="submit" value="submit" />
</form>
You need to make the array $sizes, then foreach ($sizes as $size) for your loop. THen in the echo you need this:
if(isset($_REQUEST['list'])){
switch($size[$_REQUEST['list']])
{
case "S":
$size = "Small";
break;
case "M":
$size = "Medium";
break;
case "L":
$size = "Large";
break;
}
echo $size[(int)$_REQUEST['list']] . " is equal to " . " . $size . "<br />";
}
But really you would be far better using the letters as keys for the array, and the sizes as the values: $sizes['S'] = "Small" then it would be simply in your loop
foreach ($sizes as $key => $size)
{
echo "<option value='" . $key . "'>" . $size . "</option>";
}
and to display:
$_REQUEST['list'] . " is equal to " . " . $sizes['$_REQUEST['list']] . "<br />";
You can get your desired result from rewriting your code using nested arrays for your size array like:
$size = array();
$size[] = "";
$size[] = array("S", "Small");
$size[] = array("M", "Medium");
$size[] = array("L", "Large");
if(isset($_REQUEST['list'])){
echo $size[(int)$_REQUEST['list']][0]." is equal to ". $size[(int)$_REQUEST['list']][1] ."<br />";
}
echo '<select name="list">'."\n";
$count = 0;
foreach($size as $size){
echo '<option value="'.$count.'">'.$size[0].'</option>'."\n";
$count++;
}
echo '</select>';
?>
<input type="submit" value="submit" />
</form>
Im not sure I get your problem right
But why not set the value to Small, Medium & Large while you keep the output S, M, L if you want that.
Then S = Small.
精彩评论