开发者

How to check whether dropbox item is selected?

I have a mysql database with two tables:

table1 (id, name, emailid)
table2 (id, email)
  • emailid is a relationship to table2.id

I'm trying to make an html form that lists the contents of table1 with a dropdown box for the user to select the email field. The email field is populated with <options> from table2.

My question is: how do I check what value is already selected for emailid and make that the already selected item in the dropbox when the form loads with selected="selected"?

I've gotten one solution to work already but I don't think it is the proper way to do it and was looking for the best practices method.

The way I am currently doing it will only work with one item pulled from table1 but not if there are more than 1.

Here is my code so far:

This is where I get the data to make the form:

<?php
$sql = "SELECT table1.id as table1id, table1.name, table2.id as table2id
        FROM table1
        INNER JOIN table2 ON table1.emailid = table2.id
        WHERE table1.id = {$id}"; 
$result = mysqli_query($link, $sql); 
$row = mysqli_fetch_array($result); 
$items1 = array('table1id' => $row['table1id'] 
                'name' => $row['table1.name'] 
                'table2id' =>开发者_开发问答; $row['table2id']); 

$sql = "SELECT id, email FROM table2"; 
$result = mysqli_query($link, $sql); 
$row = mysqli_fetch_array($result); 
while ($row = mysqli_fetch_array($result)) { 
    $items2[] = array('id' => $row['id'], 
                 'email' => $row['email'], 
                 'selected' => ($row['id'] == $items1['table2id']) ? ' selected="selected" ' : ''); 
}
?>

This is my HTML:

<tr> 
    <td><?php echo $items1['name']; ?></td>
    <td><input type="hidden" name="table1id" value="<?php echo $items1['table1id']; ?>" />
        <?php echo $items1['table1id']; ?></td>
    <td>
        <select name="email"> 
        <?php foreach ($items2 as $item2): ?> 
            <option value="<?php echo $item2['id']; ?>"<?php echo $item2['selected']; ?>>
            <?php echo $item2['email']; ?></option> 
        <?php endforeach; ?>
        </select>
    </td> 
</tr>

This is how the form is updated:

<?php
$id = mysql_real_escape_string($_POST['table1id']); 
$email = mysql_real_escape_string($_POST['email']); 

$sql = "UPDATE table1 SET emailid = {$email} WHERE id = {$id}"; 
?>


EDIT Killed that other stuff now that you have code up.

// Lets make our table2 the primary so we can organize our values in the $items array.
$sql = "SELECT table1.id as table1id, table1.name, table2.id as table2id, table2.email 
    FROM table2
    LEFT JOIN table1 ON table2.emailid = table1.id";
    // Do you need this WHERE?  I'm thinking not.
    // WHERE table1.id = {$id}";  
$items = array();
while($row = mysqli_fetch_array($result)) {
  // This will get set every time through the loop, but this is ok.
  // You can check to see if its set and then set or not set.
  $items[$row['table1id']]['name'] = $row['name'];
  // We will keep appending email values here to the table1id array.
  $items[$row['table1id']]['emails'][] = $row['email'];
}

Lets do the HTML markup now that we have all the items in a nice array for us.

<?php foreach($items as $id => $item): ?>    
<tr> 
  <td><?php echo $item['name']; ?></td>
  // Do you really need this hidden field?
  <td><input type="hidden" name="table1id" value="<?php echo $id; ?>" /><?php echo $id; ?></td>
  <td>
    // Updated this to be unique for each id iteration.
    <select name="email_<?php echo $id; ?>">
    // Might want to consider a is_array() check on this value.
    <?php foreach ($item['email'] as $email): ?>
        <?php $selected = $email == $_POST['email_'. $id] ? ' selected="selected"' : ''; ?>
        <option value="<?php echo $email; ?>"<?php echo $selected; ?>>
        <?php echo $email; ?></option> 
    <?php endforeach; ?>
    </select>
  </td> 
</tr>
<?php endforeach; ?>

I have not tested this so it may not work as a copy and paste. But I think this is can get you going correctly now.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜