需要从单个选择值中获取2个值。 MySQL,PHP中的XML

时间:2012-11-12 15:24:58

标签: php mysql xml forms

我有一个包含这些值的选择框:

<form action="ampcontrols.php" method="post" name="amp">
<select name="amp">
<option value="Fender '57 Deluxe","103">Fender '57 Deluxe</option>
<option value="Fender '59 Bassman","100">Fender '59 Bassman</option>
<option value="Fender '57 Champ","124">Fender '57 Champ</option>
<option value="Fender '65 Deluxe Reverb","83">Fender '65 Deluxe Reverb</option>
<option value="Fender '65 Princeton","106">Fender '65 Princeton</option>
<option value="Fender '65 Twin Reverb","117">Fender '65 Twin Reverb</option>
<option value="Fender Super-Sonic(Burn)","114">Fender Super-Sonic(Burn)</option>
<option value="British '60s","97">British '60s</option>
<option value="British '70s","121">British '70s</option>
<option value="British '80s","94">British '80s</option>
<option value="American '90","93">American '90s</option>
<option value="Metal 2000","109">Metal 2000</option>
</select>
<input name="submit" type="submit" value="Submit" />
</form>

我希望能够获得2个值并独立使用它们。这是一个不是多个选择框。 我可以回复所选的名称,即Fender '57 Deluxe对于HTML标签没有问题,但也需要传递它的相应数字,以便单独插入到xml文件中。即选择Fender '57 Deluxe作为标题,选择103作为xml的id。 我尝试使用MySQL,XML,但一次只能传递一个值!!我可能错过了一些简单的东西。 干杯

2 个答案:

答案 0 :(得分:0)

您的HTML无效。

要在单个选项上放置多个值,您可以使用数组或对象

使用数组的示例

<select name="amp">
    <option value='["Fender &apos;57 Deluxe","103"]'>Fender '57 Deluxe</option>
</select>

PHP代码

<?php
$amp = json_decode($_POST['amp']);
$amp[0] // Fender '57
$amp[1] // 103
?>

答案 1 :(得分:0)

如果我正确猜测,每个标题都由其id标识,并且您可以控制html以及PHP中的处理。在这种情况下,您不需要将标题传递回PHP处理部分;你只需要在HTML中显示它们,这样用户就可以选择&#34; Fender&#59; 59 Deluxe&#34;而不是&#34; 103&#34;。

因此,只需确保您的PHP知道哪个ID与哪个标签相关,然后您可以向用户显示标签以及向PHP / XML显示ID。

<?php
  // This array would typically be pulled from a database
  $amps = array(
    103 => "Fender '57 Deluxe",
    100 => "Fender '59 Bassman",
    124 => "Fender '57 Champ",
    83 => "Fender '65 Deluxe Reverb",
    106 => "Fender '65 Princeton",
    117 => "Fender '65 Twin Reverb",
    114 => "Fender Super-Sonic(Burn)",
    97 => "British '60s",
    121 => "British '70s",
    94 => "British '80s",
    93 => "American '90",
    109 => "Metal 2000"
  );

  // Check if the form has been submitted
  if (isset($_POST['amp'])) {
    $selected_amp = intval($_POST['amp']);
    if (array_key_exists($selected_amp, $amps)) {
      $label = $amps[$selected_amp];
      insert_xml($selected_amp, $label);
      // (todo: create function insert_xml)
    } else {
      // (todo: error handling)
    }
  }
?>
<form action="ampcontrols.php" method="post" name="amp">
  <select name="amp">
  <?php foreach ($amps as $id => $label): ?>
    <option value="<?php echo $id; ?>"><?php echo htmlentities($label); ?></option>
  <?php endforeach; ?>
  </select>
  <input name="submit" type="submit" value="Submit" />
</form>

如您所见,我喜欢在HTML中使用foreach / endforeach for PHP;如果您愿意,可以使用大括号。