Jump to content

[SOLVED] Trying to a have AJAX script work based on link clicked.


suttercain

Recommended Posts

Hi guys. I took the w3schools code and wanted to change their drop down menu to just regular links <a>

 

Here is the three files... JS, PHP and HTML

 

JS:

// JavaScript Document
var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="select.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
} 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
}
return xmlHttp;
}

 

PHP

<?php
  include('../includes/get_connected.php');  
   $q=$_GET["q"];
   $sql="SELECT * FROM tour WHERE id = '".$q."'";
   $result = mysql_query($sql);

echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";

while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['tourDate'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "</tr>";
}
echo "</table>";

?>

 

HTML (really php but treat like HTML)

<!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>
<script src="select.js" type="text/javascript"></script>
<?php 
include('../includes/get_connected.php'); 
$sql = "SELECT * FROM tour";
$results = mysql_query($sql) or die(mysql_error());
?>
</head>
<body>
<form>
<?php
while ($row = mysql_fetch_array($results)) {
echo "<a href='javascript:void(0)' onclick='showUser(this.value)'>".$row['tourDate']."</a><br />";
}
?>
</form>
<p>
<div id="txtHint"><b>User info will be listed here.</b></div>
</p>
</body>
</html>

 

I think my error is with the link itself. How do I go from the select menu to a <a herf-""></a>

 

Thanks.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.