New Coder Posted February 26, 2007 Share Posted February 26, 2007 Hello all, No doubt this type of question turns up now and then, but I have searched this site and Google but haven’t got a solution to my problem. I have a page where a user selects the department they belong to. I have a few different questions. Currently, once they have selected a department they do not get the choice again, but if they click save without selecting an option from the drop down it stores their department as “Choose Department”, which clearly isn’t a department and cannot be changed. Can I get it so if they leave the option as Choose department it won’t save and they have to pick a selection? Or is there a way so that the drop down box is always available but will display the option they have selected in the past when they re-visit? The latter is better because it will allow users to change it, if for instance they move departments. here's what i've got so far:- <form action="edit_details.php" method="post"> <?php if ($department != null) { echo ( $department); } else { $sql2="SELECT distinct department FROM dept_codes order by department"; $rs2 = mssql_query( $sql2, $conn ) ; echo ("<select name=\"department\">"); echo "<option selected>Choose Department</option>"; while($row2 = mssql_fetch_array($rs2)) { echo ("<option value=\"$row2[0]\">$row2[0]</option><br/>\r\n"); } echo ("</select>");} ?> <input type="submit" value="Save"></form> Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/ Share on other sites More sharing options...
New Coder Posted February 27, 2007 Author Share Posted February 27, 2007 Even if somebody can't help me with the code, does anybody know if it's possible? Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195064 Share on other sites More sharing options...
TRI0N Posted February 27, 2007 Share Posted February 27, 2007 A simple Validation Script will work.. At the top of your page put this in: <SCRIPT language="JavaScript"><!-- //script hider function form_validator(theForm) { if(theForm.department.value == "Choose Department") { alert("You must choose a Department."); theForm.department.focus(); return(false); } return (true); } // end script hiding --></SCRIPT> Change your form line to this: <form onSubmit="return form_validator(this)" action="edit_details.php" method="post"> Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195090 Share on other sites More sharing options...
New Coder Posted February 27, 2007 Author Share Posted February 27, 2007 Great Cheers, Is there a way of allowing the drop down box always available but to display the option thay have previously chosen? Many Thanks Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195144 Share on other sites More sharing options...
Yesideez Posted February 27, 2007 Share Posted February 27, 2007 Try this: <?php $sql2="SELECT distinct department FROM dept_codes order by department"; $rs2=mssql_query($sql2,$conn) ; echo '<select name="department">'; echo '<option>Choose Department</option>'; while ($row2=mssql_fetch_array($rs2)) { echo '<option value="'.$row2[0].'"'.($row2[0]==$department ? ' selected="selected"' : '').'>'.$row2[0].'</option>'; } echo '</select>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195156 Share on other sites More sharing options...
New Coder Posted February 27, 2007 Author Share Posted February 27, 2007 Cheers, will try new code. Should I use instead of script or aswell as? If still using script, I have some questions. function form_validator(theForm) Where it says (theForm) should I put my form name in there? and than at all other instances? I have tried both, and I can't get either to work? Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195160 Share on other sites More sharing options...
Yesideez Posted February 27, 2007 Share Posted February 27, 2007 Javascript isn't really my area, I try to avoid using it where possible as I don't like it (only because I don't know enough of it ) but it is better to verify data client-side if possible as it saves server load than verifying everything using PHP. It also saves time verifying client-side using Javascript. Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195162 Share on other sites More sharing options...
New Coder Posted February 27, 2007 Author Share Posted February 27, 2007 Thats ok, I think this may do what I want. From what I can see this is attempting to dispaly the users previous selection within the drop down box. Therefore should i be declaring what value $department will hold first? Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195178 Share on other sites More sharing options...
Yesideez Posted February 27, 2007 Share Posted February 27, 2007 If you're getting the value of $department earlier from the form then it should be set up for you already holding the name of the department. If the script is being run for the first time then it'll be empty so it should display "Select Department" as default. Try it and see how it behaves. Also might want to try removing the line that adds "Select Department" and see how you get on - if you like it you don't have to add the Javascript (this is how I do it although the only drawback is that the first option is always the default on first execution. Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195182 Share on other sites More sharing options...
New Coder Posted February 27, 2007 Author Share Posted February 27, 2007 Working a treat... Thanks They can still select Choose department as an option but if they choose another, when the page is re-visited it will show their previous selection. Javascript isn't a strong area of mine either. I'm going to play around with this little bit of javascript to see if I can get it to work but essentially it's close to the desired result. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195191 Share on other sites More sharing options...
New Coder Posted February 27, 2007 Author Share Posted February 27, 2007 I have figured it out and both are now working a treat. <SCRIPT language="JavaScript"><!-- //script hider function form_validator(theForm) { if(theForm.department.value == "") { alert("You must choose a Department."); theForm.department.focus(); return(false); } return (true); } // end script hiding --></SCRIPT> I just had to remove the value of department. At a guess, is it because I'm using a query to populate the options and "Choose department" is not brought through from the query but is hard coded? Beacause I found that if I put "Choose department" in the table it regonised it as a selection. Anyway Thanks Yesideez and TRI0N Quote Link to comment https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/#findComment-195216 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.