Getting the LINK innerhtml value from external file

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
tanvirtonu
Forum Commoner
Posts: 35
Joined: Wed Oct 17, 2007 9:15 am

Getting the LINK innerhtml value from external file

Post by tanvirtonu »

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-

Code: Select all

<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>
My customerdb.php file is as below-

Code: Select all

<?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&#058;getEditForm()'> $name </a></td>";
    $display_string .= "</tr>";
    
}
$display_string .= "</table>";
echo $display_string;
 
?>
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.
Last edited by Benjamin on Mon Jun 15, 2009 10:15 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Getting the LINK innerhtml value from external file

Post by JAB Creations »

Using innerHTML? 8O Time to do a search for JavaScript nodes.
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Re: Getting the LINK innerhtml value from external file

Post by miro_igov »

id attribute in HTML means identifier and as it's name shows it is used to identify HTML elements. You cannot have 2 different HTML elements with same identifier! If you have 2 machines in your network with same IP and you send a packet to the IP which machine will receive it? Same in HTML :wink:
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Getting the LINK innerhtml value from external file

Post by kaszu »

Change

Code: Select all

$display_string .= "<td><a id='linkname' href='javascript&#058;getEditForm()'> $name </a></td>";
into

Code: Select all

$display_string .= "<td><a href='javascript&#058;//' onclick='getEditForm(this.innerHTML);'> $name </a></td>";
Now to "getEditForm" function will be passed name as first argument.

Code: Select all

function getEditForm(name) {
    alert(name);
}
tanvirtonu
Forum Commoner
Posts: 35
Joined: Wed Oct 17, 2007 9:15 am

Re: Getting the LINK innerhtml value from external file

Post by tanvirtonu »

Thanx a lot brother actuall I tried with this.id and it worked but at first for some reason it didn't work.Can you tell me what properties/attributes can I call with "this" keyword and the scope of this "this" in an inline javascript.Can I call any attribute of an element using "this'.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Getting the LINK innerhtml value from external file

Post by kaszu »

'this' is referring to the <a> link (that is the scope) on which user clicks, so you can do everything what can be done with <a> node.
Post Reply