jamichelli Posted April 3, 2009 Share Posted April 3, 2009 I'm working on getting my forms to properly validate. I already have the text and textarea fields where they repopulate with the previous input should some other part of the form fail to validate. The code for that goes like this: // if there were errors, re-populate the form fields if (!empty($errors)) { $fields = $_POST; } and then: <input type="text" size="60" name="user_name" id="user_name"tabindex="1" value="<?=$fields['user_name']?>" /> Again, no problems with the text parts of the form repopulating upon validation failure...I'm just having a hard time doing the same thing with the select part of the form. Any help here? I'm trying to keep it as simple as possible. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/152469-drop-down-list-form-helper/ Share on other sites More sharing options...
Yesideez Posted April 3, 2009 Share Posted April 3, 2009 I'm just off to bed but one last post doesn't hurt! I'd gather the contents from the form regardless of whether the form validates or not. That way you can post the data the user entered straight back in the form should they need to edit anything. As for the select part... $intSelectID=intval($_POST['myselect']); We can get the option they've chosen from our select box. Now to build the select box (from contents from the database) and make their selection the default one - we'll use a city selection box as an example. $htmCities=''; $query=-mysql_query("SELECT id,city FROM cities ORDER BY city ASC"); while ($row=mysql_fetch_assoc($query)) { $htmCities='<option valud="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'.$row['city'].'</option>'; } Then we use that in our form: <select name="myselect"><?php echo $htmCities; ?></select> Hope that helps. Quote Link to comment https://forums.phpfreaks.com/topic/152469-drop-down-list-form-helper/#findComment-800744 Share on other sites More sharing options...
jamichelli Posted April 4, 2009 Author Share Posted April 4, 2009 Ok...here's what I have. I'm actually doing this for a state list. The problem that I'm having is that it only brings up the last state in my db...any clues? code: <?php $con = mysql_connect("localhost","dbname","dbpassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbname", $con); $state=''; $query= mysql_query("SELECT id,state FROM `states` ORDER BY state ASC"); while($row = mysql_fetch_assoc($query)) { $state='<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'. $row['state'].'</option>'; } ?> <select name=”state”><?php echo $state; ?></select> Any help on this would be greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/152469-drop-down-list-form-helper/#findComment-801017 Share on other sites More sharing options...
jamichelli Posted April 4, 2009 Author Share Posted April 4, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/152469-drop-down-list-form-helper/#findComment-801103 Share on other sites More sharing options...
ignace Posted April 4, 2009 Share Posted April 4, 2009 The reason you are getting one thing and one thing only is because you are always overwriting your container: $state='<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'. $row['state'].'</option>' should be: $state.='<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'. $row['state'].'</option>' notice the dot after $state now you concatenate your strings, i used the shorthand version though the long version is: $state= $state . '<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'. $row['state'].'</optio a mistake both Yesideez and you made! Quote Link to comment https://forums.phpfreaks.com/topic/152469-drop-down-list-form-helper/#findComment-801104 Share on other sites More sharing options...
jamichelli Posted April 4, 2009 Author Share Posted April 4, 2009 Thanks for the help ignace...now, what about this: $intSelectID=intval($_POST['myselect']); Now that I have this corrected and have the full list available, I just need to put this last part in...where ever it may go...and then be done with it. I may try to figure that part out after a good night's...or day's...sleep. Thanks again Yesideez and ignace! Quote Link to comment https://forums.phpfreaks.com/topic/152469-drop-down-list-form-helper/#findComment-801150 Share on other sites More sharing options...
jamichelli Posted April 9, 2009 Author Share Posted April 9, 2009 Ok...still having trouble and need a little help. This is for a drop down list. Where would I place the code snippet at the end of this post so that the selected value remains whenever the form fails validation and the user needs to fill in other parts of the form before re-submitting? [code: <?php $con = mysql_connect("localhost","dbname","dbpassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbname", $con); $state='; $query= mysql_query("SELECT id,state FROM `states` ORDER BY state ASC"); while($row = mysql_fetch_assoc($query)) { $state.='<option value="'.$row['id].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'. $row['state'].'</option>'; } ?> <select name=”state”><?php echo $state; ?></select> code] Where does this go???: $intSelectID=intval($_POST['state']); Quote Link to comment https://forums.phpfreaks.com/topic/152469-drop-down-list-form-helper/#findComment-805850 Share on other sites More sharing options...
jamichelli Posted April 10, 2009 Author Share Posted April 10, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/152469-drop-down-list-form-helper/#findComment-806054 Share on other sites More sharing options...
xtopolis Posted April 10, 2009 Share Posted April 10, 2009 before the while loop $state.='<option value="'.$row['id'].'"'.($row['id']==$intSelectID ? ' selected="selected"' : '').'>'. This code inside the while loop is telling it to basically: if the current row in the database(while building the selects options) has an id value that matches the $intSelectID value (which you gave it from $_POST['state']), then add the text 'selected="selected"' otherwise add nothing, making it retain it's value. I think that's accurate, it's kinda jumbled together .. Quote Link to comment https://forums.phpfreaks.com/topic/152469-drop-down-list-form-helper/#findComment-806058 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.