Page 1 of 1

choosing the default value from a dropdown list...

Posted: Tue Jul 12, 2005 2:42 am
by pleigh
i would like to know how to do this...i have a dropdown list with 3 options...the first choose an option, 2nd is option1, 3rd is option2...now, upon inserting to the database, i like the default option(choose an option) not to be written to the database and instead, a message will prompt the user to select one option...so far my dropdown code is below..any modifications that will help is very much appreciated...thanks in advance..

Code: Select all

//check for category of organization
	if (empty($_POST['category']))
	{
		$c = false;
		$message .= 'Please select at least one category<br>';
	}
	else
	{		
		$c = $_POST['category'];
	}

Posted: Tue Jul 12, 2005 4:47 am
by harrisonad
use SELECTED on the option you want to be defaulted.

Code: Select all

$str = "
<select>
<option value='general_cat' SELECTED>General Category</option>
<option value='math_cat'>Math</option>
<option value='science_cat'>Science</option>
</select>";
echo $str;
You won't need anymore isset() checking with this trick.

Posted: Tue Jul 12, 2005 4:49 am
by pleigh
ok harrisonad...i'll try...thanks a lot pal...:D

Posted: Tue Jul 12, 2005 7:17 am
by pleigh
nope...this style didn't work in my code...anymore tips guys??

Posted: Tue Jul 12, 2005 1:04 pm
by John Cartwright
change line 2 to

Code: Select all

if ($_POST['category'] == 'choose a category')

Posted: Tue Jul 12, 2005 8:17 pm
by harrisonad
pleigh wrote:nope...this style didn't work in my code...
Do you mind if you'll explain why it didn't work?
Post you code rather.

Posted: Tue Jul 12, 2005 11:55 pm
by pleigh
thanks guys...so far i did this

Code: Select all

if ($_POST['companyrole'] == 0)
	{
		$r = false;
		$message .= 'Please select at least one role<br>';
	}
	else
	{		
		$r = $_POST['companyrole'];
	}
and this on the form

Code: Select all

<td width="70%"><select name="companyrole">
                      <option value="0" selected>Please choose a role</option>
                      <option>Contractor</option>
                      <option>Sub-Contractor</option>
                      <option>Outsourcing</option>
                    </select></td>
when i tried not to select anything, it perfectly worked...unfortunately, when i tried to supply or select a detail, the error message of $message appears and prompting me to select at least one role....

Posted: Wed Jul 13, 2005 12:02 am
by harrisonad
pleigh wrote: <option value="0" selected>Please choose a role</option>
<option>Contractor</option>
<option>Sub-Contractor</option>
<option>Outsourcing</option>
You sure did set the default value for your selectbox but did not put any more options. Only the default option has a value attribute while the others don't have.

You need to change that part into ...
I wrote: <option value="0" selected>Please choose a role</option>
<option value="1">Contractor</option>
<option value="2">Sub-Contractor</option>
<option value="3">Outsourcing</option>
Or any value that is appropriate to each option.

hope that helps. :roll:

Posted: Wed Jul 13, 2005 12:24 am
by pleigh
yes thanks harrisonad....if i had this

Code: Select all

<option value="0" selected>Please choose a role</option>
<option value="1">Contractor</option>
<option value="2">Sub-Contractor</option>
<option value="3">Outsourcing</option>
instead of seeing the "contractor" word in the database, i see the "1" value...what should be done by here??

Posted: Wed Jul 13, 2005 12:35 am
by harrisonad

Code: Select all

echo "
<select name='companyrole'>
<option value=''> - Select Company Role - </option>
<option value='Contractor'>Contractor</option>
<option value='Sub-Contractor'>Sub-Contractor</option>
<option value='Outscoring'>Outscoring</option>
</select>";

Posted: Wed Jul 13, 2005 12:44 am
by pleigh
thanks harris...it worked great...:D