php - How to check whether dropbox item is selected? -
i have mysql database 2 tables:
table1 (id, name, emailid) table2 (id, email)
emailid
relationshiptable2.id
i'm trying make html form lists contents of table1 dropdown box user select email field. email field populated <options>
table2
.
my question is: how check value selected emailid
, make selected item in dropbox when form loads selected="selected"
?
i've gotten 1 solution work don't think proper way , looking best practices method.
the way doing work 1 item pulled table1
not if there more 1.
here code far:
this data make form:
<?php $sql = "select table1.id table1id, table1.name, table2.id table2id table1 inner join table2 on table1.emailid = table2.id 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 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 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 $item2): ?> <option value="<?php echo $item2['id']; ?>"<?php echo $item2['selected']; ?>> <?php echo $item2['email']; ?></option> <?php endforeach; ?> </select> </td> </tr>
this how form updated:
<?php $id = mysql_real_escape_string($_post['table1id']); $email = mysql_real_escape_string($_post['email']); $sql = "update table1 set emailid = {$email} id = {$id}"; ?>
edit killed other stuff have code up.
// lets make our table2 primary can organize our values in $items array. $sql = "select table1.id table1id, table1.name, table2.id table2id, table2.email table2 left join table1 on table2.emailid = table1.id"; // need where? i'm thinking not. // table1.id = {$id}"; $items = array(); while($row = mysqli_fetch_array($result)) { // set every time through loop, ok. // can check see if set , set or not set. $items[$row['table1id']]['name'] = $row['name']; // keep appending email values here table1id array. $items[$row['table1id']]['emails'][] = $row['email']; }
lets html markup have items in nice array us.
<?php foreach($items $id => $item): ?> <tr> <td><?php echo $item['name']; ?></td> // need hidden field? <td><input type="hidden" name="table1id" value="<?php echo $id; ?>" /><?php echo $id; ?></td> <td> // updated unique each id iteration. <select name="email_<?php echo $id; ?>"> // might want consider is_array() check on value. <?php foreach ($item['email'] $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 may not work copy , paste. think can going correctly now.
Comments
Post a Comment