开发者

I want to add and remove items from a menu with PHP

开发者 https://www.devze.com 2023-01-03 11:34 出处:网络
This menu will need to be updated daily. <html><head></head><body> <h1> Welcome to Burgerama </h1>

This menu will need to be updated daily.

<html><head></head><body>
<h1> Welcome to Burgerama </h1>

<?php include("menuBuilder.php");
showBurgerMenu();
?>

</body></html>

Menu items are stored in the database. Item开发者_如何学Pythons have a display field; if it is on, the item should be displayed on the menu. The menu only displays 4 or 5 "specials" at a time, and the manager needs to change menu items easily. I want to make a menu editing page like this:

<?php include("burger_queries.php");
dbconnect("burger_database");

foreach($menuItem in burger_database)
{
    echo createToggleButton($menuItem);
}
?>

.. with a toggle button for each menu item. Ideally the button will be labeled with the menu item, in blue if the item is "on", and red if the item is "off." Clicking the button switches between "on" and "off"

I am stuck trying to a get a button to execute an UPDATE query on its corresponding menu item.


To do that, each button will need to send a form that will then execute the script, be it with ajax or a regular refresh. An example of what the html should look like for a disabled button called Field 1 would be this:

<form id='testform' action='test.php' method='post'>
<input type='hidden' name='who' value='field_1' />
<input type='hidden' name='what' value='enable' />
<div class='disabled' onclick='testform.submit()'>Field 1</div>
</form>

Note that it has two hidden fields that contain the information needed by your php code to know which field needs actions and what are those actions. Also see that I've added a class to the div container that will be customizable with css to achieve the desired red or blue background color. Something as simple as padding:10px;background-color:red; should do the trick for you although you might want to style it a little further. To send the form we used javascript, but you could replace the div for a submit button if you want to.

To test if this works, place the following code at the beginning of the file:

if(!empty($_POST)){
if(!empty($_POST['who']) && !empty($_POST['what'])){
  switch($_POST['what']){
    case 'enable':
      echo 'Actions to enable field '.$_POST['who'];
    break;
    case 'disable':
      echo 'Actions to disable field '.$_POST['who'];
    break;
  }
}
}

This allows you to perform the update queries for your buttons at the right time. Note that if you are not planning on using ajax you should refresh the page before you can see the effects of your update.

I will not go any further as on how to build the html example from php since I assume you have basic knowledge and are able to deduct it.

Hope it helps, have a nice day.


The button should submit a form or do an AJAX request, which is handled by your PHP script. The form contains the identifier of the product to toggle. The script then does the update query, updating this product.


If I understand this problem correctly, your problem is that you want a button to run a method that executes an UPDATE query, which means the problem is HTML based, correct?

Unfortunately, my PHP is rusty, so I won't be able to give good advice on the method itself, but the HTML button should have (aside from runat="server") the attribute OnClick="MenuMethod", MenuMethod being the method with the UPDATE query, and it should be found in your "burger_queries.php" page.

Also, it might be a good idea to place the AutoPostback="true" attribute in your buttons so that your menu will update itself as soon as you press the button.

0

精彩评论

暂无评论...
验证码 换一张
取 消