underlink Posted August 6, 2013 Share Posted August 6, 2013 Not sure how to go about this task but I imagine it requires some sort of foreach loop I have a drop down list populated by a label called LST_Type, this table only exists to provide options for this dropdown list The value selected on the dropdown menu gets written to a table called "assets". when the users clicks away from the form (using ajax) Here is what I need to happen: On page load the dropdown list need its default option to be whatever the current value of the record is in the "asset" table. As the only way you can insert data into this field is via the dropdown list it will always be somethig from the "LST_Type" table The reason is that the data is written to the fields when you click away from the input boxes and unless you are editing the "Type" record it updates it to its default selection. hope this makes sense here is my code: <tr id="<?php echo $id; ?>" class="edit_tr"><!-- Title Colum --><td class="style4" style="font-size:14px;width:200; height:35px; class="edit_td"> Type: </td><td style="font-size:14px;width:270px;border:solid 0px #000;padding:0px; class="edit_td"><span style="color:#0066CC;" id="type1_<?php echo $id; ?>" class="text"><?php echo $Type; ?></span><!-- ***************************************START - This Is the dropdown menu script *********************************************** --> <script type="text/javascript"> function OnDropDownChange2(dropDown) { var selectedValue = dropDown.options[dropDown.selectedIndex].value; document.getElementById("type1_input_<?php echo $id; ?>").value = selectedValue; } </script><select name = "MYtype" id="type1_input_<?php echo $id; ?>" class="editbox" onChange="OnDropDownChange2(this);">><?php$sql = mysql_query("SELECT Type FROM LST_Type");while ($row = mysql_fetch_array($sql)){echo "<option value='" . $row['Type'] . "'>" . $row['Type'] . "</option>";}?></select><input type="text" value="<?php echo $Type; ?>" class="editbox" id="type1_input_<?php echo $id; ?>" /> /><!-- ***************************************END *********************************************** --> Quote Link to comment Share on other sites More sharing options...
abrahamgarcia27 Posted August 6, 2013 Share Posted August 6, 2013 Try something like this echo '<option value="'.$row['Type'].'" '.(($row['Type']==$record[data_value])?'selected="selected"':"").'>'.$row['Type'].'</option>'; Quote Link to comment Share on other sites More sharing options...
underlink Posted August 6, 2013 Author Share Posted August 6, 2013 Thanks I acctualy got it working doing something like that. However It has caused another issue. When I try and copy the code onto another dropdown menu (obviously altering the values the second one does not work. It is most likly a stupid error I am not spotting <tr id="<?php echo $id; ?>" class="edit_tr"> <!-- Title Colum --><td class="style4" style="font-size:14px;width:200; height:35px; class="edit_td"> Type: </td> <td style="font-size:14px;width:270px;border:solid 0px #000;padding:0px; class="edit_td"> <span style="color:#0066CC;" id="type1_<?php echo $id; ?>" class="text"><?php echo $Type; ?></span> <!-- ***************************************START - This Is the dropdown menu script *********************************************** --> <script type="text/javascript"> function OnDropDownChange2(dropDown) { var selectedValue = dropDown.options[dropDown.selectedIndex].value; document.getElementById("type1_input_<?php echo $id; ?>").value = selectedValue; } </script> <select name = "MYtype" selected="selected" id="type1_input_<?php echo $id; ?>" class="editbox" onChange="OnDropDownChange2(this);"> <?php $sql = mysql_query("SELECT LType FROM LST_Type"); while ($row = mysql_fetch_array($sql)){ if ($row['LType'] == $Type) { echo "<option Selected='selected' value=" . $row["LType"] . ">" . $row['LType'] . "</option>"; } else echo "<option value=" . $row["LType"] . ">" . $row['LType'] . "</option>"; } ?> </select> <input type="text" value="<?php echo $Type; ?>" class="editbox" id="type1_input_<?php echo $id; ?>" /> /> I changed the $Type to $Cond (as well as other "type" attributes the list for Cond (short for Condition) is in a lable called LST_Cond with a column "LCond" can anyone spot why this wont work the second time around (on the same page) Quote Link to comment Share on other sites More sharing options...
Solution underlink Posted August 6, 2013 Author Solution Share Posted August 6, 2013 Never mind, the problem was with the MYSQL table not sure what but i deleted it and recreated it and all started working Quote Link to comment 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.