Creating a Dropdown That Refreshs the Page and Allow User to Edit Specific Parts of Database
I understand the title may be a little vague, but, hopefully, I can explain it here.
We have a page where the user modifies certain fields of a database. The fields being modified change with the selection of a dropdown at the top of the page.
Suppose we have this table by the name of restaurants
:
-----------------------------------------------------------
| id | name | items |
-------------------------------------------------------------
| 1 | fryking | burger, fries, shake |
| 2 | burgertown | nuggets, chicken sandwich |
-----------------------------------------------------------
When loaded, the page should look like this:
[ CHOOSE RESTAURANT ] [v]
ITEMS:
With "fryking" selected, the user can: view fryking's items, add new items, remove unwanted items.
[ fryking ] [v]
ITEMS:
[ (txtbox) ] [ADD ITEM]
1. burger [DELETE]
2. fries [DELETE]
3. shake [DELETE]
Once a restaurant has been selected, the user should be able to add, delete, etc. without the dropdown reverting to "SELECT RESTAURANT" and forcing them to select it again.
Right now, we can display the appropriate information when a restaurant has been selected, but not have the restaurant remain selected after processing an "add" or "delete".
Sorry I couldn't explain this开发者_如何学编程 any better. I know this has been done 1000 times on other sites, so someone must understand what I'm looking for.
Please let me know if I can clarify anything.
Many thanks,
Justian Meyer
EDIT:
Here's how the deletes are being handled (each in their separate forms).
$page = str_replace('%7E', '~', $_SERVER['REQUEST_URI']);
...
<form method="post" action="'.$page.'">
<input type="hidden" name="item_id" value="'.$i.'"> // $i refers to the value from the loop
<input type="submit" name="delete_item" value="Delete">
</form>
What could I do to condense every button into one form and still process them properly?
JavaScript: - set the value of the drop down list in the onload event.
PHP: - set the selected tag by saying something like
<?
if ($_POST)
{
$ddlRestaurantPersistValue = $_POST['ddlRestaurant'];
}
?>
and the HTML would be
<select id="ddlRestaurant" name="ddlRestaurant">
// build the options here if you are doing a database while loop
// check to see if the option value == $ddlRestaurantPersistValue and then add
// selected='selected' to the attributes of the option
</select>
I dont know what technology you use, but in asp.net mvc i would do something like this
when performing httppost i would save the value (or id or whatever) of the selected dropdown item in a session (or even a variable in the controller) and give it back to the view after the post and set that session or model information as the default.
can you tell what technology you are using?
I don't quietly get what you mean.
Once a restaurant has been selected, the user should be able to add, delete, etc. without the dropdown reverting to "SELECT RESTAURANT" and forcing them to select it again.
Considering I don't get why SELECT RESTAURANT mades a comeback, why just don't select what you want with a javascript code?
You can bind an onchange listener
Well, I think something like this is best handled via AJAX calls, but I won't suggest you re-write the page just to fix one problem.
You are going to have to tell your form processor what the current restaurant is, so it can be set correctly after the save. Assuming you have many forms on the page for a good reason, you are going to have to suck it up and put a hidden restaurant
input in every form. Update them all when the user changes the restaurant value (via javascript) and reset the restaurant when building the page after the save.
精彩评论