Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/40189-solved-drop-down-box-from-table-help/
Share on other sites

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">

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>';
?>

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?

 

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 :D) 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.

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.

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.