Forum Moderators: coopster

Message Too Old, No Replies

PHP mySQL form question

         

RussellC

9:38 pm on Nov 13, 2002 (gmt 0)

10+ Year Member



I want an administrator of my app I am builing to be able to edit client information. I made a page that when you click 'update' it grabs all of the selected clients info from the DB and puts them into form fields so all youhave to is change it and then click the update button and it updates the info. I got his working perfectly.

Now my question is, As far a drop downs go, is there a way to make the option that is already stored in the database for the current client show up as the initially selected item in the dropdown?

Example: Administrator clicks to edit a client, and they get to the form page there is a dropdown that says: Active Client? with the answers Yes and No. If No is already stored in the DB how can I make sure that is the initially selected item in the dropdown. Same goes for 'Yes' if that is the value already stored in the DB.

Am I making any sense.

Thanks for any help,
Russell

andreasfriedrich

10:09 pm on Nov 13, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the standard HTML ways to preselect those option elements [w3.org].

<form> 
<select size="1" name="form[active]">
<option value="1" <?php
if ($is_active) echo 'selected="selected"';
?>>YES</option>
<option value="0"<?php
if (!$is_active) echo 'selected="selected"';
?>>NO</option>
</select>

Andreas

lars stecken

10:13 pm on Nov 13, 2002 (gmt 0)

10+ Year Member



Yes, it is possible. Easier done with Session vars because if the user updates the from and there are any failures you can put the changes he already made in the Session vars and display them back to him.

If you're not working with session vars the procedure is the same. Like this if you have only a few options:

<select>
<option value="1" <?if (isset($dbvalue) && $dbvalue == "1") {?>SELECTED<?}?>>1</option>
<option value="2" <?if (isset($dbvalue) && $dbvalue == "2") {?>SELECTED<?}?>>1</option>
</select>

If you have a longer list loading from the db it goes like this, assuming you have all values in an array:

<select>
for ($i = 0; $i < count($dbarray);$i++) {
<option value="<? echo $dbarray[$i]?>" <?if (isset($dbvalue) && $dbvalue == $dbarray[$i]{?>SELECTED<?}?>><? echo $dbarray[$i]?></option>
}
</select>

$dbvalue of course is the value you load from your database.

I hope this helped,
greets, Stefan

jatar_k

11:00 pm on Nov 13, 2002 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld [webmasterworld.com] lars stecken

RussellC

11:13 pm on Nov 13, 2002 (gmt 0)

10+ Year Member



Perfect! I will try this out shortly. Thanks a million!

can i do this:

<form>
<select size="1" name="form[active]">
<option value="1"
<?php
if ($active == 1) {
echo 'selected="selected"';
?>
>Active</option>
<option value="0"
<?php
if ($active == 2)
echo 'selected="selected"';
?>
>Inactive</option>
<option value="0"
<?php
if ($active == 3)
echo 'selected="selected"';
?>
>Completed</option>
</select>

anyone know if that will work?

lars stecken

8:39 am on Nov 14, 2002 (gmt 0)

10+ Year Member



There's no need to "echo 'selected="selected"'. Instead 'echo "SELECTED"' is sufficient. Maybe you want to check though if your $active var has been set or not, just for good programming style (isset($active)).
If it's not set it always triggers a PHP warning (ok, it's not being displayed if your error reporting is not set to E_ALL).
Curious question: Why are you naming your select-tag "name[active]"? This will return an array with one item since you have a drop down menu and not a multiple select menu...
Are you putting all your form vars in the array "form"?

Greets,Stefan

andreasfriedrich

12:49 pm on Nov 14, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no need to "echo 'selected="selected"'

unless you want your HTML code to be XHTML compliant: XML does not support attribute minimization [w3.org].

Instead 'echo "SELECTED"' is sufficient

unless you want your HTML code to be XHTML compliant: All element and attribute names must be lower case [w3.org].

can i do this:

If you remove the opening brace in if ($active == 1) { then that code will work. You might want to use values for the value attribute that correspond to the content in each option element.

Andreas

RussellC

3:28 pm on Nov 14, 2002 (gmt 0)

10+ Year Member



I got it to work this morning. Thanks everyone for the help!