user1589936
user1589936

Reputation: 55

Two select forms connected to different fields of the DB

i need 2 similar select options in the same form but both r linked 2 different fields on the database can anyone correct my code. i don't need 2 submit buttons i just put in there just for better usability my code is

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

Upvotes: 0

Views: 270

Answers (3)

srinath
srinath

Reputation: 2998

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

Upvotes: 0

tjk
tjk

Reputation: 194

Your initial code appears to be missing a closing "select" tag which may be causing the syntax error.

Instead, you could try a somewhat cleaner approach by only using PHP where necessary (saves escaping all of the quotation marks).

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

In addition, you could check whether the form has been submitted - then validate and process the input.

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

}
?>

Upvotes: 1

srinath
srinath

Reputation: 2998

The question looks bit ambiguos to me . But to my understanding, you can do something like this

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

in output.php

<?php
$box1 = $_POST['box1'];
$box2 = $_POST['box2'];
?>

Upvotes: 0

Related Questions