skip to Main Content

I want to pass drop down menu item name as an input to a php file. As an example, Tutorials menu item has three drop down menu items (Photoshop,Illustrator,Web Design) and when chick one of them I want to display some data retrive from a database.

enter image description here

To to that I think I have to pass the type of the menu item to a php file. But the problem is how can I get the name of the menu item to a variable.
Is this possible? and how can I do this?

Following is my code for the ‘Tutorials’.

<nav>
<ul>
        <li><a href="#">Tutorials</a>
            <ul>
                <li><a href="#">Photoshop</a></li>
                <li><a href="#">Illustrator</a></li>
                <li><a href="#">Web Design</a></li>
            </ul>
        </li>
</ul>

2

Answers


  1. Short answer in JavaScript:

    <script type="text/javascript>
        var selected_menu_item;
        $('nav ul ul li').click(function() {
            selected_menu_item = $( this ).text();
        });
    </script>
    

    Now you have the clicked item’s name in the selected_menu_item javascript variable.

    So, once you have it, you can use it in http calls:

    <script type="text/javascript>
        var selected_menu_item;
    
        $('nav ul ul li').click(function() {
            // Get the clicked item name
            selected_menu_item = $( this ).text();
    
            // Send it somewhere by POST
            $.post("http://example.com", {item: selected_menu_item});
        });
    </script>
    

    This assumes that you’re using jQuery – a very popular JavaScript library, which has now become a standard on the web. In order to make this example work, put:

    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
    

    In your HEAD section. There’s no need for additional files because it uses a CDN to deliver that file to you from the web.

    See JSFiddle to test it for yourself: https://jsfiddle.net/1o49a2rs/

    Login or Signup to reply.
  2. You can achieve this really easily with GET method, You can read further information about the GET Method on PHP.net. Just search GET.
    I will try to explain how GET suits your needs simply and convincing, any doubts just ask and I’ll expand the answer with my knowledge about it.

    Your links would look something like this:

    <nav>
    <ul>
        <li><a href="#">Tutorials</a>
            <ul>
                <li><a href="/php/PostData.php?link=Photoshop">Photoshop</a></li>
                <li><a href="/php/PostData.php?link=Illustrator">Illustrator</a></li>
                <li><a href="/php/PostData.php?link=WebDesign">Web Design</a></li>
            </ul>
        </li>
    </ul>
    

    In your PostData.php file you need to create a small function to GET the information sent with this method. (PostData.php?link=Photoshop): ‘link’ would be your GET variable and youre sending its value as photoshop.

    This is a simple way to GET the value in PHP:

    if ($_SERVER['REQUEST_METHOD'] == "GET") { // Checks if GET Request was made.
        if($_GET['link']){                     // Checks if link variable is set throguh GET
            $getLink = $_GET["link"];          // Sets the value of link to a variable
            if($getLink == "Photoshop"){       // if the value of the link is Photoshop
                echo "Youve selected Photoshop"; // Echoes you selected it.
            } else {
                echo "You didn't select Photoshop"; // Options are infinite past here
            }
        }
    }
    

    I hope this helped!. Good luck and anything else, just ask.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search