zelig Posted November 22, 2011 Share Posted November 22, 2011 Okay, so, here's the scenario. I have a form that is editing an item that is already in the database. The text fields fill in just fine with that info. However, the drop down menus don't retrieve that info, rather resorting to the defaults, which can be a problem if you don't remember what you originally had. Is there anyway to make the dropdown menus pull the info from the table and use that rather than resorting back to the default? I tried using this: <tr><td width="20%">Bonus:</td><td><select name="bonus" value="{{bonus}}"> <option value="Attack" {{bonus1select}}>Add to the attack power of weapon</option> <option value="Defense" {{bonus2select}}>Add to the defensive power of armor</option> <option value="None" {{bonus3select}}>No effect</option> </select><br /></td></tr> So, it's obvious the "value" portion not working. Any help would be great!! Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/ Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 You mean setting the default selected value to match the database? Your {$bonus1select} and {$bonus2select} look like they are setup to do this. However, I don't know why you are doing {{$variableName}}. Please post a greater portion of your code. I'm pretty sure that <select doesn't have a value attribute. Whatever the default is has to be selected by either (1) Displaying it first in the options list or (2) giving it the "selected" attribute in the <option tag. Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/#findComment-1290535 Share on other sites More sharing options...
zelig Posted November 22, 2011 Author Share Posted November 22, 2011 Here's my coding so far: function edititem($id) { if (isset($_POST["submit"])) { extract($_POST); $errors = 0; $errorlist = ""; if ($name == "") { $errors++; $errorlist .= "Name is required.<br />"; } if ($price == "") { $errors++; $errorlist .= "Price is required.<br />"; } if (!is_numeric($price)) { $errors++; $errorlist .= "Price must be a number.<br />"; } if ($type == "") { $errors++; $errorlist .= "Type is required.<br />"; } if (!is_numeric($value)) { $errors++; $errorlist .= "Value must be a number.<br />"; } if ($errors == 0) { $query = doquery("UPDATE `items` SET `name`='$name', `descript`='$descript', `price`='$price', `type`='$type', `bonus`='$bonus', `slot`='$slot', `target`='$target', `attr`='$attr', `value`='$value' WHERE `id`='$id'", "items"); admindisplay("Item updated.","Edit Items"); } else { admindisplay("<b>Errors:</b><br /><div style=\"color:red;\">$errorlist</div><br />Please go back and try again.", "Edit Items"); } } $query = doquery("SELECT * FROM items WHERE id='$id'", "items"); $row = mysql_fetch_array($query); $page = <<<END <b><u>Edit Items</u></b><br /><br /> <form action="admin_panel.php?do=edititem:$id" method="post"> <table width="90%"> <tr><td width="20%">Type:</td><td><select name="type" value="{{type}}"> <option value="Armor" {{type1select}}>Armor</option> <option value="Item" {{type2select}}>Item</option> <option value="Pet" {{type3select}}>Pet</option> <option value="Potion" {{type4select}}>Potion</option> <option value="Weapon" {{type5select}}>Weapon</option> </select><br /><span class="small">Choose what type the item is: Items = RP only items that will not be "used".</span></td></tr> </table> <input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /> </form> END; if ($row["type"] == 1) { $row["type1select"] = "selected=\"selected\" "; } else { $row["type1select"] = ""; } if ($row["type"] == 2) { $row["type2select"] = "selected=\"selected\" "; } else { $row["type2select"] = ""; } if ($row["type"] == 3) { $row["type3select"] = "selected=\"selected\" "; } else { $row["type3select"] = ""; } if ($row["type"] == 4) { $row["type4select"] = "selected=\"selected\" "; } else { $row["type4select"] = ""; } if ($row["type"] == 5) { $row["type5select"] = "selected=\"selected\" "; } else { $row["type5select"] = ""; } $page = parsetemplate($page, $row); admindisplay($page, "Edit Items"); } Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/#findComment-1290543 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 A better way might be: $options = array(1 => "Armor", 2 => "Item", 3 => "Pet, 4 => "Potion", 5 => "Weapon"); $selectCode = "<select name=\"type\">"; foreach ($options as $key => $value) { $selected = ""; if ($row['type'] == $key) $selected = " selected"; $selectCode .= "<option value=\"{$value}\"{$selected}>{$value}</option>"; } $selectCode = "</select>"; $page = <<<END <b><u>Edit Items</u></b><br /><br /> <form action="admin_panel.php?do=edititem:$id" method="post"> <table width="90%"> <tr><td width="20%">Type:</td><td> {$selectCode} <br /><span class="small">Choose what type the item is: Items = RP only items that will not be "used".</span></td></tr> </table> <input type="submit" name="submit" value="Submit" /> <input type="reset" name="reset" value="Reset" /> </form> END; There are a lot of ways you can do this, though. I don't really think you need to be running a function to replace those variables. You're just making more work for yourself. Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/#findComment-1290547 Share on other sites More sharing options...
unlishema.wolf Posted November 22, 2011 Share Posted November 22, 2011 $selectCode = "</select>"; should be $selectCode .= "</select>"; Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/#findComment-1290552 Share on other sites More sharing options...
zelig Posted November 22, 2011 Author Share Posted November 22, 2011 If I have multiple drop down menus, would this still work? Just change it to $options1, $options2, etc.? Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/#findComment-1290554 Share on other sites More sharing options...
teynon Posted November 22, 2011 Share Posted November 22, 2011 $selectCode = "</select>"; should be $selectCode .= "</select>"; Good catch. If I have multiple drop down menus, would this still work? Just change it to $options1, $options2, etc.? Why wouldn't it? Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/#findComment-1290555 Share on other sites More sharing options...
zelig Posted November 22, 2011 Author Share Posted November 22, 2011 Just making sure. Thanks! Let me plug this in and see if I am golden. Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/#findComment-1290558 Share on other sites More sharing options...
unlishema.wolf Posted November 22, 2011 Share Posted November 22, 2011 yes it would. You could even make a function to do it so you don't have to post the code multiple times. Note: A little rusty on functions so recode if needed. function getSelectCode($options) { $selectCode = "<select name=\"type\">"; foreach ($options as $key => $value) { $selected = ""; if ($row['type'] == $key) $selected = " selected"; $selectCode .= "<option value=\"{$value}\"{$selected}>{$value}</option>"; } $selectCode = "</select>"; return $selectCode; } I think that is correct. The return statement may be wrong. Just because I have been doing java coding a lot lately lol. @teynon thank you. Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/#findComment-1290559 Share on other sites More sharing options...
zelig Posted November 22, 2011 Author Share Posted November 22, 2011 Nice. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/251631-drop-down-menu-help/#findComment-1290564 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.