View Full Version : How to get variable from "Vert Nav" list item

01-16-2012, 09:48 PM
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/

<!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">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<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>

<script type="text/javascript">
function changeList(a)
document.write("Value=" + a);
// window.location=location;
return false;


01-16-2012, 11:17 PM
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:

<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>

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.

01-17-2012, 02:00 AM
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:thumbsup: