0

我需要相同形式的 2 个类似的选择选项,但两者都 r 链接了数据库上的 2 个不同字段,任何人都可以更正我的代码。我不需要2个提交按钮,我只是为了更好的可用性而放在那里我的代码是

echo "
      <form method=\"post\" action=\"".$_SERVER['PHP_SELF']."\">
      <p><strong>Event Time (hh:mm):</strong><br/>
      <select name=\"event_time_hh\">";
      for ($y=1; $y<=24; $y++){
       echo "<option value=\"$y\">$y</option>;

     <p><strong>Event end (hh:mm):</strong><br/>
     <select name=\"event_end_hh\">";
     for ($y=1; $y<=24; $y++){
       echo "<option value=\"$y\">$y</option>";
}
echo "</select> :
      <select name=\"event_end_mm\">
      <option value=\"00\">00</option>
      <option value=\"15\">15</option>
      <option value=\"30\">30</option>
      <option value=\"45\">45</option>
      <input type=\"submit\" name=\"submit\" value=\"Add Event!\">";
echo "</select> :
      <select name=\"event_time_mm\">
      <option value=\"00\">00</option>
      <option value=\"15\">15</option>
      <option value=\"30\">30</option>
      <option value=\"45\">45</option>
      <input type=\"submit\" name=\"submit\" value=\"Add Event!\">
</form>";
4

3 回答 3

1

您的初始代码似乎缺少可能导致语法错误的关闭“选择”标记。

相反,您可以尝试一种更简洁的方法,仅在必要时使用 PHP(保存转义所有引号)。

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p><strong>Event Time (hh:mm):</strong></p></br />
<select name="event_time_hh">
    <?php
        for ($y=1; $y<=24; $y++) {
            echo "<option value=\"$y\">$y</option>";    
        }
    ?>
</select>

<p><strong>Event end (hh:mm):</strong></p></br />
<select name="event_time_hh">
    <?php
        for ($x=1; $x<=24; $x++) {
            echo "<option value=\"$x\">$x</option>";    
        }
    ?>
</select>

<select name="event_time_mm">
    <option value="00">00</option>
    <option value="15">15</option>
    <option value="30">30</option>
    <option value="45">45</option>
</select>

<select name="event_end_mm">
    <option value="00">00</option>
    <option value="15">15</option>
    <option value="30">30</option>
    <option value="45">45</option>
</select>

<input type="submit" name="submit" value="Add Event" />
</form>

此外,您可以检查表单是否已提交 - 然后验证并处理输入。

<?php

// Check to see if form has been submitted.
if (isset($_POST['submit'])) {

    /*  Some validation should be performed here to ensure the following
     *  values have actually been selected prior to form being submitted.
     */  
    $event_time_hh = $_POST['event_time_hh'];
    $event_time_mm = $_POST['event_time_mm'];
    $event_end_hh = $_POST['event_end_hh'];
    $event_end_mm = $_POST['event_end_mm'];

}
?>
于 2012-08-31T11:23:59.493 回答
0

index.php echo ' '; for($y=1;$y<=24;$y++) echo "$y";

echo '</select>';

echo '
<select name="box2"> ';
for($y=1;$y<=24;$y++)
echo "<option value='$y'>$y</option>";
echo '
</select>

<input type="submit" value="submit">
</form> ';

output.php

$box1_value = $_POST['box1'];
$box2_value = $_POST['box2'];

Now, box1 and box2 has the selected values of 2 respective drop down boxes. Hope it meets your requirement

于 2012-08-31T11:13:32.083 回答
0

这个问题在我看来有点模棱两可。但据我了解,你可以做这样的事情

<form action="output.php method="post">
<select name="box1">
// here you populate from db field1
</select>

<select name="box2">
// here you populate from db field2
</select>

<input type="submit" value="submit>

</form>

在 output.php 中

<?php
$box1 = $_POST['box1'];
$box2 = $_POST['box2'];
?>
于 2012-08-31T10:46:05.363 回答