Select Box

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
mystramagic2
Forum Newbie
Posts: 1
Joined: Tue Nov 02, 2004 1:11 am

Select Box

Post by mystramagic2 »

Hi

I have an input form into a database. Some of the fields are stored via a select box. This works fine. Then I need to have an output form where the fields that are stored with the select box is also shown in the output form with the select box. Problem is, i can get it to show in a normal textbox, but when i try the select box, it doesnt show which value was selected in the input form.

My select box has the following values: Canvas, Exhibition, Phone-In and so forth.

All the fields in the database is selected in the output form and i put them into an array.
Eg. Listing No is a textbox and Source is a select box:

the code is:

Code: Select all

$query=" SELECT * FROM property WHERE ListingNo='$ListingNo'";
$result=mysql_query($query);
$num=mysql_num_rows($result);

while ($row = mysql_fetch_array($result)){ 
echo 'echo '<input type="text" name="ListingNo" size="20" value="'.$row&#1111;ListingNo].'">';
echo '<SELECT class="clsselectbox" name="cboSource" size="1" value="'.$row&#1111;Source].'">
<OPTION></OPTION><OPTION>Canvas</option><OPTION>Exhibition</option><OPTION> Phone-In</option>';
&#125;
The listing no works fine. Just cant get the form to select the selected value from the database.

Could somebody maybe help me on this?

Oh another quick question i have is it possible for the values that was stored in a textbox, to be shown in a checkbox. My professor told us that the input form should have textboxes, then with the output form, he wants checkboxes to show if their was something entered. or something.
Like if the user entered that there was a jacuzzi at the residence, then the output form should have a checked checkbox for jacuzzi. (but i dont see how this is possible since the textbox can save yes there was a jacuzzi or no there wasnt)

Thanx for all the help
Mystra 8)
dreamline
Forum Contributor
Posts: 158
Joined: Fri May 28, 2004 2:37 am

Post by dreamline »

You forgot to print the selected statement after:

echo('<option value="" selected>');
?>

I would also consider using an array and use a number in your database..
If you use an array to print the values on screen then you can use a for loop to determine which one is selected. You use the loop to print all options and if the number in your database is equal to the one in the array you print <option ...... selected>.

:D

?>[/php_man]
User avatar
AnarKy
Forum Contributor
Posts: 119
Joined: Tue Nov 02, 2004 1:49 am
Location: South Africa

Post by AnarKy »

Hi Mystra,

I think the problem with your select box values is that the
value="'.$row[ListingNo].'

is in the wrong place. :(

It should be placed into the <OPTION > tag... i.e. AND you need an ‘if’ statement to have the value in the d/base as the selected value by default.

Something like this would be needed.

Code: Select all

<?
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
&#123;
?>    <OPTION value=  <?=$row&#1111;'ListingNo']?> 
                <?= ( $row&#1111;'ID']== $ListingNo ) ? 'selected' : "" ?>
        ></OPTION>
	
<? &#125;
:!: in this Scenario, $result holds all possible values of ListingNo's which are stored in the database... not just those as in your query.

The question mark just before the 'selected' is like an 'if' statement :)

Hope this helps a bit.

AnarKy (Terrence).
Post Reply