Display db drop down list item on form with php

software development

#1

I have created a “Back Office” where users can update their personal information. On the “Update Address” page, I pre-populate the fields with information currently in the database. I am running into a problem with the “State” drop-down list. I would like to display the one from the database, but also have the entire list available in the drop down if they need to change it. I know how to indicate this via html by coding (selected=“selected”) for the item I want to display as selected on initial load. How can I do with with php? I tried saying value = <?php echo $row........; ?> and that displayed the value from the database correctly when the page loaded. But, if I change the state to something else and click the “Update” button, the form re-displays (because I am not re-directing them to any other pages after a successful update) and the “old” state selection is still selected in the drop down. If I relead the page, the “new” state displays correctly from the database. Any help is GREATLY appreciated.

Thanks,
Fred


#2

Lets see how quick, and very dirty I can explain it.
Load the states into an Array (size 100).

then Loop through the array of state fields, echo out a value= part for each of them, check if it’s equal to their state, if it is do a selected=“selected”. Then reguardless echo the “>”.

-Jason

Dreamhost Hosting - Last one out, turn off the lights. Alternative.


#3

Thanks Jason. I was able to make this work - the newly selected state displays after the “Update”. However, I was also printing out a text message on the page letting the user know the update was successful. Here is the code I currently have in my form after validation is successful:

//Print a message on the page letting the user know the update was successful.
print ‘

You have successfully updated your Address.

’;
header(‘Location: updateAddress.php’);

If I take out the header('Location:…) line, the form will not show the newly selected state - it shows the old state. It will display the message though. If I leave the header('Location:…) line in, it shows the newly selected state but doesn’t display the message for the user.

Is there a way to make this work?

I also considered popping up a small javascript window saying “Update Successful”:

print ‘’;
header(‘Location: updateAddress.php’);

If I take out the header('Location:…) the new window pops up, but again, the form isn’t re-read so the newly selected state doesn’t display on the form when you close the pop-up. If I leave in the header('Location…), the form displays the newly selected state, but doesn’t pop up the javascript window.

Any additional help is greatly appreciated.

Thanks,
Fred


#4

How are you processign the form? Post? Java?

a header(‘Location: updateAddress.php’);

Automatically goes to the new page, discarding the rest of the page. The easiest way would be replace the location: with

which will display the "update notice" for 4 seconds then go to the new page.

But, the way I usualy do it is have the form post back to it’s self. Checking to see if data has been posted, and if it has, having it react differently (if that makes any sense) without using location, or redirects.

-Jason

Dreamhost Hosting - Last one out, turn off the lights. Alternative.


#5

I am using Post and putting the current page name in to submit it back to itself:

.

I don’t really want to go to a new page, I want to re-display the current page with the newly updated information submitted by the user. If I didn’t put in a header(‘Location: updateAddress.php’); in, it displayed the message but left the “old” information on the form…even though the database was updated. I found that if I hit “Refresh” on the page or just clicked the page link again, the “New” information was displayed. So, I put in the header(Location:…) to refresh the page. Maybe I shouldn’t be doing it this way? Is there a better way?

You mentioned “Checking to see if data has been posted…”. Can you explain that further? Maybe this is what I am looking for. How do you check to see if data has been posted?

Again, I really appreciate your help on this Jason.

  • Fred

#6

I just do an isset on one of the posted fields.
if (isset($_POST[‘Name’])) {
// display updated information
} else {
// display form
}

-Jason

Dreamhost Hosting - Last one out, turn off the lights. Alternative.