In my html form there are some customer names which are loaded as the html body loads.The names are shown as blank link.I want to get the name of each customer as the link is clicked.Customer names are filled with another php file-customerdb.php. This is my form- <html> <head> <script type="text/javascript" > var xmlhttp = false; if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); } function getCusName() { xmlhttp.open("GET","customerdb.php",true); xmlhttp.onreadystatechange = updateName; xmlhttp.send(null); } function updateName() { if(xmlhttp.readyState == 4 && xmlhttp.status == 200) { response=xmlhttp.responseText; document.getElementById("showCustomer").innerHTML = response; } } function getEditForm() { var val=document.getElementById("linkname").innerHTML; } </script> </head> <body onload="getCusName()"> <div id="showCustomer">Content for id "showCustomer" Goes Here</div> </body> </html> HTML: My customerdb.php file is as below- <?php $dbhost="localhost"; $dbuser="root"; $dbpass="admin"; $dbname="classicmodels"; //Connect to MySQL Server $con=mysql_connect($dbhost,$dbuser,$dbpass,$dbname); //Select Database mysql_select_db($dbname,$con) or die( mysql_error()); //build query $query = "SELECT customerName FROM customers LIMIT 0,10"; $qry_result = mysql_query($query) or die(mysql_error()); //Build Result String $display_string = "<table>"; $display_string .= "<tr>"; $display_string .= "<th><b>Customer Names</b></th>"; $display_string .= "</tr>"; // Insert a new row in the table for each person returned while($row = mysql_fetch_array($qry_result)){ $name=$row[customerName]; $display_string .= "<tr>"; $display_string .= "<td><a id='linkname' href='javascript:getEditForm()'> $name </a></td>"; $display_string .= "</tr>"; } $display_string .= "</table>"; echo $display_string; ?> PHP: I can show the customer names as Httplink and upon clicking getEditForm() javascript function is called.But I want to get the name of each customer for each link upon clicking.I only get first customer name upon clicking.
You are giving all your links the same ID (linkname), so all your getElementById("linkname") calls get the same innerHTML. Why not just pass the name of the customer directly to the getEditForm() function: $display_string .= "<td><a href=\"javascript:getEditForm('$name')\"> $name </a></td>"; PHP: