目标HTML选项标签,而不是其在PHP中的值


target HTML option label instead of its value in PHP?

我试图在PHP页面中以选项标签为目标,而不是其值。

我知道PHP是服务器端的,可能很难针对选项标签,但有解决办法吗?

我使用的代码是:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <link rel="stylesheet" type="text/css" href=
        "css/mainstyle_css.css" />
        <meta http-equiv="Content-Type" content=
        "text/html; charset=utf-8" />
        <title>
          title
        </title>
      </head>
      <body>
    <form method="post" action="">
      <select name="myList" id="myList" class="myList">
        <optgroup label="list 1">
    <option value="music/one" label="techno">techno</option>
    <option value="music/two" label="rock">rock</option>
    </optgroup>
      </select>
    <input type="submit" name="submit" id="submit" class="submit" value="Search"/>
    </form>
     <?php echo $myList; ?>

    </body>
</html>

通过上面的代码,我在php页面上获得了music/one和music/two的echoed。我需要做的是呼应他们的标签,"技术"answers"摇滚"。

有人能帮我一下吗?

您可以这样做:

<?php
$options = array();
$options["music/one"] = "techno";
$options["music/two"] = "rock";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <link rel="stylesheet" type="text/css" href="css/mainstyle_css.css" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>title</title>
    </head>
    <body>
        <form method="post" action="">
            <select name="myList" id="myList" class="myList">
                <optgroup label="list 1">
                    <?php
                    foreach($options as $key => $value)
                    {
                        echo '<option value="'. $key .'" label="'. $value .'">'.$value.'</option>';
                    }
                    ?>
                </optgroup>
            </select>
            <input type="submit" name="submit" id="submit" class="submit" value="Search"/>
        </form>
        <?php echo $options[$_POST['myList']]; ?>
    </body>
</html>

试试这个:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <link rel="stylesheet" type="text/css" href=
    "css/mainstyle_css.css" />
    <meta http-equiv="Content-Type" content=
    "text/html; charset=utf-8" />
    <title>
      title
    </title>
  </head>
  <body>
<form method="post" action="a.php">
  <select name="myList" id="myList" class="myList">
    <optgroup label="list 1">
<?php
  foreach($options as $key => $value)
                {
                    echo "<option value='{$key}' label='{$value}'>{$value}</option>"; // this will fill all your values in option field via loop
                }
                ?>
<option value="music/one" label="techno">techno</option>  // you can also do it this way manually as u did.
<option value="music/two" label="rock">rock</option>
</optgroup>
  </select>
<input type="submit" name="submit" id="submit" class="submit" value="Search"/>
</form>
 </body>
</html>
// below codeshould be in a.php
<?php if(isset($_POST['submit']))
{
  echo $_POST['myList']; 
}
?>