Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

johnw65's avatar

Drop Down Menu: Keep it persistent after ajax load

When the user logs in, I have an ajax to populate a drop down menu which will stay the same until the user logs out.

Unfortunately, after the going to another link, the drop down menu gets refreshed and shows no record. Since the menu is going to stay the same, I want to load the drop down once. I'm unable to use session data since it gets lost sometimes.

What is the best method of maintaining the drop down menu without having to query the database each time.

0 likes
2 replies
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

One possible solution to maintain the drop-down menu without querying the database each time is to use local storage or cookies to store the menu data.

Here's an example of how you can implement this solution using JavaScript:

// Check if the menu data is already stored in local storage
if (localStorage.getItem('menuData')) {
  // If it exists, retrieve the data and populate the drop-down menu
  const menuData = JSON.parse(localStorage.getItem('menuData'));
  populateDropDown(menuData);
} else {
  // If it doesn't exist, make an AJAX request to fetch the menu data
  // and store it in local storage for future use
  fetchMenuData()
    .then((menuData) => {
      localStorage.setItem('menuData', JSON.stringify(menuData));
      populateDropDown(menuData);
    })
    .catch((error) => {
      console.error('Error fetching menu data:', error);
    });
}

function fetchMenuData() {
  return new Promise((resolve, reject) => {
    // Make an AJAX request to fetch the menu data
    // Replace the URL with your actual API endpoint
    fetch('/api/menu')
      .then((response) => response.json())
      .then((data) => resolve(data))
      .catch((error) => reject(error));
  });
}

function populateDropDown(menuData) {
  // Populate the drop-down menu with the menu data
  const dropdown = document.getElementById('dropdown');

  menuData.forEach((item) => {
    const option = document.createElement('option');
    option.value = item.value;
    option.textContent = item.label;
    dropdown.appendChild(option);
  });
}

In this solution, we first check if the menu data is already stored in local storage using localStorage.getItem('menuData'). If it exists, we retrieve the data and populate the drop-down menu using the populateDropDown function. If it doesn't exist, we make an AJAX request to fetch the menu data, store it in local storage using localStorage.setItem('menuData', JSON.stringify(menuData)), and then populate the drop-down menu.

Note that you'll need to replace /api/menu with the actual URL of your API endpoint that returns the menu data. Also, make sure to update the populateDropDown function to match your HTML structure and how you want to populate the drop-down menu.

Remember to include this JavaScript code on the pages where the drop-down menu is present.

MohamedTammam's avatar

Store it in the local storage and clear it when the user logs out and/or a new user logs in.

Please or to participate in this conversation.