簡體   English   中英

目標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頁面上得到了音樂/一個和音樂/兩個回聲。 我需要做的是呼應他們的標簽,即“ techno”和“ rock”。

有人可以幫我這個忙嗎?

您可以執行以下操作:

<?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']; 
}
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM