Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts

    How to get variable from "Vert Nav" list item

    Hello. I am new to php, so I don't mean to sound ignorant (although, I can find my way around). I have a vertical nav bar in my html page. Rather than than click on a link and go to "that" page, I would like to just pull the "value" from the selected "list item" and use it as a variable in my php.

    My goal is to "acquire" that value so I can use it in my php on this page. (I want to be able to use it in an sql query on this page).

    I made an attempt using javascript, and I think the stratedgy might be similar using php.

    Could someone please steer me in a direction? I would greatly appreciate it, Buffmin.
    I have included my code, but you can see it at http://test.cnjwebsolutions.com/

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
     
    <body>
    <ul> 
    <li><a>Main Menu</a> 
    <ul class="leftbutton" > 
    <li><a href="Item1" onclick="return changeList(this);">Item 1</a></li> 
    <li><a href="Item2" onclick="return changeList(this);">Item 2</a></li> 
    <li><a href="Item3" onclick="return changeList(this);">Item 3</a></li> 
    <li><a href="Item4" onclick="return changeList(this);">Item 4</a></li> 
    <li><a href="Item5" onclick="return changeList(this);">Item 5</a></li> 
    </ul> 
    </li> 
    </ul> 
     
     
     
    <script type="text/javascript"> 
    function changeList(a) 
    { 
    document.write("Value=" + a);
    // window.location=location; 
    return false; 
    } 
    </script> 
     
    </body>
    </html>
    Last edited by Spookster; 01-16-2012 at 11:23 PM. Reason: Fixed closing CODE tag

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Your javascript solution is the first step towards a more AJAX solution, which is fine.

    To do it purely with PHP, I would use each link to give some sort of value. Like:
    Code:
    <ul>
    <li><a>Main Menu</a>
    <ul class="leftbutton" >
    <li><a href="index.php?item=1" onclick="return changeList(this);">Item 1</a></li>
    <li><a href="index.php?item=2" onclick="return changeList(this);">Item 2</a></li>
    <li><a href="index.php?item=3" onclick="return changeList(this);">Item 3</a></li>
    <li><a href="index.php?item=4" onclick="return changeList(this);">Item 4</a></li>
    <li><a href="index.php?item=5" onclick="return changeList(this);">Item 5</a></li>
    </ul>
    </li>
    </ul
    This sends the value of item over using GET. So, you would access the value using the GET array ($_GET['item']). If you're using it in a mysql_query(), remember mysql_real_escape_string() for sql injection prevention.
    Useful function to retrieve difference in times
    The best PHP resource
    A good PHP FAQ
    PLEASE remember to wrap your code in [PHP] tags.
    PHP Code:
    // Replace this
    if(isset($_POST['submitButton']))
    // With this
    if(!empty($_POST))
    // Then check for values/forms. Some IE versions don't send the submit button 
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.

  • Users who have thanked BluePanther for this post:

    Buffmin (01-17-2012)

  • #3
    Regular Coder
    Join Date
    Aug 2011
    Posts
    192
    Thanks
    112
    Thanked 0 Times in 0 Posts
    Ahh, thank you so much! That is so cool. I really appreciate it. Now, I can try and get my page working. Thanks again, and thanks for your tips. Buffmin


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •