shure2 Posted April 3, 2010 Share Posted April 3, 2010 Hi, I have scoured the net and can't find an answer so I wondered if you guys can help. I have a combo box that selects data from a database, I can then add the selection of the combo box and other fields to another table, however when I do this with php the page refreshes and the value of the combo box changes back to the default order (where I want it to show the option I just added to the database) I have tried the below code, theoretically I thought it might work but it doesn't (the leadProductCategory is the combo box selection that I add to the database - it is set in the php file where I add to the database). $selectLeadProductCatQuery = "SELECT * FROM category where userid = '".$_SESSION['userid']."' ORDER BY .'".$_SESSION['leadProductCategory']."'; $selectLeadProductCatResult = mysql_query($selectLeadProductCatQuery) or die(mysql_error()); while($row1LeadProductCat=mysql_fetch_array($selectLeadProductCatResult)) { echo '<option value="'.$row1LeadProductCat['catname'].'">'.$row1LeadProductCat['catname'].'</option>'; } Quote Link to comment Share on other sites More sharing options...
shure2 Posted April 3, 2010 Author Share Posted April 3, 2010 I solved it by putting the value into a session variable when its added to the database and then used the following code: <select name="leadprodcategory"> <?php $selectLeadProductCatQuery = "SELECT * FROM category where userid = '".$_SESSION['userid']."' ORDER BY catname"; $selectLeadProductCatResult = mysql_query($selectLeadProductCatQuery) or die(mysql_error()); while($row1LeadProductCat=mysql_fetch_array($selectLeadProductCatResult)) { echo '<option value="' . $row1LeadProductCat['catname'].'"'; if( $row1LeadProductCat['catname'] == $_SESSION['leadProductCategory'] ) { //change 'select_name' to the value of your <select name=" echo ' selected="selected"'; } echo '>' . $row1LeadProductCat['catname'] . '</option>'; } ?> </select> hope this is useful for someone else! Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 3, 2010 Share Posted April 3, 2010 This has been asked and answered many times on this forum. Here's a modification of your code that is cleaned up and more secure <select name="leadprodcategory"> <?php $userID = mysql_real_escape_string($_SESSION['userid']); $query = "SELECT catname FROM category where userid = '{$userID}' ORDER BY catname"; $result = mysql_query($selectLeadProductCatQuery) or die(mysql_error()); while($record = mysql_fetch_assoc($result)) { $selected = ($record['catname'] == $_SESSION['leadProductCategory']) ? ' selected="selected"': ''; echo "<option value=\"{$record['catname']}\"{$selectred}>{$record['catname']}</option>\n"; } ?> </select> Quote Link to comment Share on other sites More sharing options...
shure2 Posted April 5, 2010 Author Share Posted April 5, 2010 This has been asked and answered many times on this forum. Here's a modification of your code that is cleaned up and more secure <select name="leadprodcategory"> <?php $userID = mysql_real_escape_string($_SESSION['userid']); $query = "SELECT catname FROM category where userid = '{$userID}' ORDER BY catname"; $result = mysql_query($selectLeadProductCatQuery) or die(mysql_error()); while($record = mysql_fetch_assoc($result)) { $selected = ($record['catname'] == $_SESSION['leadProductCategory']) ? ' selected="selected"': ''; echo "<option value=\"{$record['catname']}\"{$selectred}>{$record['catname']}</option>\n"; } ?> </select> thank you for the advice and better technique, I modified my code and it works great, however I have a combo box which is slightly different and I'm struggling to do what I have with you code above to it. <select name="leadid" onchange="showLeadName()"> <?php $userID = mysql_real_escape_string($_SESSION['userid']); $query = "SELECT * FROM lead where userid = '{$userID}' ORDER BY leadname"; $result = mysql_query($selectLeadQuery) or die(mysql_error()); while($record=mysql_fetch_array($result)) { ?> <?php echo '<option value="'.$record['leadid'].'">'.$record['leadname'].'</option>'; ?> <?php } ?> </select> Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 You are defining the string "$query", but then trying to run a query against the string "$selectLeadQuery" - which, presumably, does not exist. <select name="leadid" onchange="showLeadName()"> <?php $userID = mysql_real_escape_string($_SESSION['userid']); $query = "SELECT `leadid`, `leadname` FROM `lead` WHERE userid = '{$userID}' ORDER BY `leadname`"; $result = mysql_query($query) or die(mysql_error()); while($record=mysql_fetch_array($result)) { echo "<option value=\"{$record['leadid']}\">{$record['leadname']}</option>\n"; } ?> </select> Quote Link to comment Share on other sites More sharing options...
shure2 Posted April 5, 2010 Author Share Posted April 5, 2010 You are defining the string "$query", but then trying to run a query against the string "$selectLeadQuery" - which, presumably, does not exist. <select name="leadid" onchange="showLeadName()"> <?php $userID = mysql_real_escape_string($_SESSION['userid']); $query = "SELECT `leadid`, `leadname` FROM `lead` WHERE userid = '{$userID}' ORDER BY `leadname`"; $result = mysql_query($query) or die(mysql_error()); while($record=mysql_fetch_array($result)) { echo "<option value=\"{$record['leadid']}\">{$record['leadname']}</option>\n"; } ?> </select> yes you are right, i started changing the code to reflect your advice but missed that variable, could you help me change the code like the other list box so that the session['leadname'] appears in the listbox as a default. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 5, 2010 Share Posted April 5, 2010 while($record=mysql_fetch_array($result)) { $selected = ($record['leadname']==$_SESSION['leadname']) ? ' selected="selected"' : ''; echo "<option value=\"{$record['leadid']}\"{$selected}>{$record['leadname']}</option>\n"; } 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.