matthewst Posted June 5, 2007 Share Posted June 5, 2007 I have a form that autopopulates and allows user to make changes where necessary. In this form are some drop down menus (hardcoded on the page). All the selections (50 states for example) are there. If I select a state it will then post to the database. If I don't select a state and just leave it as the previously selected state (not change the selection already made by a user) it puts a blank in the database. Examples: The <?="$rest_state_pr"?> will pull the state the user selected previously. If I change the slelection it saves the new selection, but if I leave it as the "default" It puts a blank in the database. Drop Down: <td><select name="rest_state_pr" class="formTextbox" value="<?="$rest_state_pr"?>"> <option value=""><?="$rest_state_pr"?></option> <option value="Alabama">Alabama</option> <option value="Alaska">Alaska</option> <option value="Arizona">Arizona</option> <option value="Arkansas">Arkansas</option> <option value="California">California</option> <option value="Clorado">Colorado</option> </select></td> Form and Query: <form name="FormName" action="table_order_handler.php" enctype="multipart/form-data" method="post"> <?php $query="SELECT * FROM abc_tables WHERE table_id=$table_id"; $result=mysql_query($query); while ($row = mysql_fetch_assoc($result)) { $rest_state_pr = $row['rest_state_pr'] } ?> Handler: <?php $query_update="UPDATE abc_tables SET rest_state_pr='$rest_state_pr' WHERE table_id='$table_id'"; mysql_query($query_update) or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
matthewst Posted June 5, 2007 Author Share Posted June 5, 2007 I had to set the "default" (selection pulled from the database) as selected. Changed this: <option value=""><?="$rest_state_pr"?></option> To this: <option value="<?php echo $rest_state_pr; ?>" selected><?php echo $rest_state_pr; ?> Quote Link to comment Share on other sites More sharing options...
Yesideez Posted June 5, 2007 Share Posted June 5, 2007 What exactly are you trying to achieve? Are you trying to make it so that if the user doesn't select anything it puts something else in the database instead or something else? I don't quite understand what you're getting at. If you surround your code with [code] and [/code] tags it will make your code more easier to read. I'll try and answer your question though I would create a table in the database called `states` and have the fields like this: stateid TINYINT unsigned autoincrement state VARCHAR(40) Have the first entry something like this: stateid=1 state='???' Then add the rest of the states after that one. The code to get the state from the form and to build the drop-down box would be something like this: <?php $intStateID=$_POST['optstate']; //GET CHOSEN STATE FROM THE FORM $optStates='<option value="1">???</option>'; $query=mysql_query("SELECT * FROM `states` WHERE `stateid`>'1' ORDER BY `state` ASC"); while ($fetch=mysql_fetch_assoc($query)) { $optStates.='<option value="'.$fetch['stateid'].'"'.($intStateID==$fetch['stateid'] ? ' selected="selected"' : '').'>'.$fetch['state'].'</option>'; } if ($_POST['subsenddata']) { //USER IS SUBMITTING USING THE FORM mysql_query("UPDATE `table` SET `stateid`='".$intStateID."' WHERE `user`='".$userid."'"); } Then the HTML for the form would be something like this: <form action="<?=$_SERVER['PHP_SELF']?>" method="post"> State: <select name="optstate"><?=$optStates?></select><br /> <input type="submit" name="subsenddata" value="Send Data" /> </form> This way the drop-down box is always populated using the contents of the `states` table. While the routine is building the drop-down box it constantly checks if any of them match what the user has selected. If it matches then it marks it as selected so it is automatically selected and displayed in the form. If noe of them match then '???' is displayed instead. If the user doesn't select a state then the value 1 is used instead which reflects the '???' Quote Link to comment Share on other sites More sharing options...
Yesideez Posted June 5, 2007 Share Posted June 5, 2007 Seems you solved it just before I added my bit - well done 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.