向PHP表单添加其他功能

时间:2011-06-10 19:56:00

标签: php forms

根据下面的代码,当我从下拉框[S,M,L]中选择一个值并点击提交时,我得到以下输出之一:

S is equal to
M is equal to
L is equal to

我希望输出符合

的要求
S is equal to Small 
M is equal to Medium 
L is equal to Large

可以在我的代码中添加一些内容来实现此目的吗?或者我需要采取不同的方法吗?

<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 '<select 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">

5 个答案:

答案 0 :(得分:3)

为什么不使用关联数组,然后你不必乱用整数或$count

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

输出看起来像这样:

S is equal to Small

+------------+-+
| Choose one |▼|
+------------+-+
| S - Small    |
| M - Medium   |
| L - Large    |
+--------------+

答案 1 :(得分:1)

你需要为你的循环制作数组$ sizes,然后foreach($ sizes as $ size)。在回声中你需要这个:

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 />"; 

}

但实际上你会更好地使用字母作为数组的键,大小作为值:$ sizes ['S'] =“小”然后它只是在你的循环中

foreach ($sizes as $key => $size)
{
echo "<option value='" . $key . "'>" . $size . "</option>";
}

并显示:

$_REQUEST['list'] . " is equal to " . " . $sizes['$_REQUEST['list']] . "<br />"; 

答案 2 :(得分:1)

使用此解决方案,您可以重复使用原始静态数组来填充后期回声。同时尽量避免在html中使用\ n而是使用语义<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>

答案 3 :(得分:1)

您可以使用嵌套数组为您的大小数组重写代码来获得所需的结果,如:

$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>

答案 4 :(得分:0)

我不确定我的问题是否正确

但为什么不将值设置为Small,Medium&amp;如果你想要的话,保持输出S,M,L很大。

然后S =小。