Page 1 of 1

Getting the LINK innerhtml value from external file

Posted: Sat Jun 13, 2009 4:50 pm
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.

Re: Getting the LINK innerhtml value from external file

Posted: Sat Jun 13, 2009 5:31 pm
by JAB Creations
Using innerHTML? 8O Time to do a search for JavaScript nodes.

Re: Getting the LINK innerhtml value from external file

Posted: Sun Jun 14, 2009 7:26 am
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:

Re: Getting the LINK innerhtml value from external file

Posted: Sun Jun 14, 2009 11:06 am
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);
}

Re: Getting the LINK innerhtml value from external file

Posted: Sun Jun 14, 2009 2:33 pm
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'.

Re: Getting the LINK innerhtml value from external file

Posted: Mon Jun 15, 2009 11:59 am
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.