Hello everyone,
I have a problem with my drop down list box on my edit page in php. I would like my drop down list box to show what the user has selected, then if the user wishes to, change their selection updating the DB(mysql) at the same time. I have a couple of radio buttons that does the same thing and works fine.

eg <?php if ($dataArow['garageopt'] == "yes"){ echo ' checked="checked"';}?> />

When i try to apply the same method changing the checked=checked to selected it doesen't work.

<select name="cartype" />   
<option value="<?php if ($dataArow['cartype'] == "tvr"){ echo ' selected="selected"';}?>" />tvr</option>
<option value="<?php if ($$dataArow['cartype'] == "lexus"){ echo 'selected';}?>"/>lexue</option>
<option value="<?php if ($$dataArow['cartype'] == "porche"){ echo 'selected';}?>"/></option>
</select>

Can somebody help me please it would be much appreciated.

Thanks.

Recommended Answers

All 9 Replies

try:

<select name="cartype" />   
<option value="tvr" <?php if ($dataArow['cartype'] == "tvr"){ echo ' selected="selected"';}?> />tvr</option>
<option value="lexus" <?php if ($dataArow['cartype'] == "lexus"){ echo 'selected="selected"';}?> />lexus</option>
<option value="porche" <?php if ($dataArow['cartype'] == "porche"){ echo 'selected="selected"';}?> /></option>
</select>

try:

<select name="cartype" />   
<option value="tvr" <?php if ($dataArow['cartype'] == "tvr"){ echo ' selected="selected"';}?> />tvr</option>
<option value="lexus" <?php if ($dataArow['cartype'] == "lexus"){ echo 'selected="selected"';}?> />lexus</option>
<option value="porche" <?php if ($dataArow['cartype'] == "porche"){ echo 'selected="selected"';}?> /></option>
</select>

Thanks for your help, but it doesn't seem to work quit as much as i would like it to. The DB updates thats great, but when the edit page reloads the drop down list box goes back to the first option (tvr),and not to the one that was selected for update eg porche.

Well, let's run a little test. After the select, add this bit of code:

<!-- <?php echo $dataArow['cartype']; ?> -->

That way, we'll see just what $dataArow contains. If it is returning empty, then nothing in the code would be set to "selected", therefore, the first option would always be selected.

Well, let's run a little test. After the select, add this bit of code:

<!-- <?php echo $dataArow['cartype']; ?> -->

That way, we'll see just what $dataArow contains. If it is returning empty, then nothing in the code would be set to "selected", therefore, the first option would always be selected.

Hi Wraithmanilian,
The code you provided show no change, on reload, the first option is returned (tvr).

Hi Wraithmanilian,
The code you provided show no change, on reload, the first option is returned (tvr).

With that page rendered, right-click in your browser and view the source. Look under the select and see what is posted between the <!-- and -->

With that page rendered, right-click in your browser and view the source. Look under the select and see what is posted between the <!-- and -->

Nothing.

Nothing.

Then THAT means that the variable $dataArow is empty, and since it has to equal SOMETHING in order to choose what sets the selected="selected", then nothing gets chosen, therefore, nothing changes.

The problem is that you are returning zero records. It's a MySQL issue.

Then THAT means that the variable $dataArow is empty, and since it has to equal SOMETHING in order to choose what sets the selected="selected", then nothing gets chosen, therefore, nothing changes.

The problem is that you are returning zero records. It's a MySQL issue.

Got it workig now. Thanks for your advice.

Got it workig now. Thanks for your advice.

You're welcome. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.