josh000 0 Newbie Poster

Hello.

I have a basic php page which displays rows of a mysql db, and when clicking on them will use ajax to display the remaining data. What I would like to do is pass the primary key from the row to the ajax function/php page, but I am usnure how to do this. The id is called article_no but is not selected as part of the query in the first page.

This is my code:

<html><head><title>Ebay Auctions</title>

<link rel="stylesheet" href="test.css" type="text/css" />

</head>

<script type="text/javascript" src="ajaxlib.js"></script>

<body>

<?php

$db_host = 'localhost';

$db_user = 'root2';

$db_pwd = 'geheim';

$database = 'ebay';

$table = 'Auctions';

if (!mysql_connect($db_host, $db_user, $db_pwd))

    die("Can't connect to database");

if (!mysql_select_db($database))

    die("Can't select database");

// sending query

$result = mysql_query("SELECT SELLER_ID, ACCESSSTARTS, ARTICLE_NAME FROM {$table}");

if (!$result) {

    die("Query to show fields from table failed");

}

$fields_num = mysql_num_fields($result);

echo '<div id="Layer0">';

echo '<div id="Layer1">';

echo "	<li>Link 1</li>

 		<li>Link 2</li>

		<li>Link 3</li> ";

echo '</div>';

echo '<div id="Layer2">Content goes here</div>';

echo '<div id="Layer3">';

echo "<h1>Table: {$table}</h1>";

echo "<table border='1'><tr>";

// printing table headers

for($i=0; $i<$fields_num; $i++)

{

    $field = mysql_fetch_field($result);

    echo "<td width='33%'>{$field->name}</td>";

}

echo "</tr>\n";

// printing table rows

while($row = mysql_fetch_row($result))

{

    echo "<tr>";

    // $row is array... foreach( .. ) puts every element

    // of $row to $cell variable

    foreach($row as $cell)

        echo '<td><a href="#" onclick="GetAuctionData()">' . $cell . '</a></td>';

    echo "</tr>\n";

	echo "</div>";

	echo "</div>";

}

mysql_free_result($result);

?>

</body></html>

and Javascript

var xmlHttp

function GetAuctionData()

{

xmlHttp=GetXmlHttpObject()

if(xmlHttp==null)

{

alert("Your browser is not supported?")

}

var url="get_auction.php?"

url=url+"cmd=GetAuctionData"

url=url+"&sid="+Math.random()

xmlHttp.onreadystatechange = FetchComplete;

xmlHttp.open("GET",url,true)

xmlHttp.send(null)

}

function FetchComplete()

{

if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

{

document.getElementById("Layer2").innerHTML=xmlHttp.responseText

}

if(xmlHttp.readyState==1 || xmlHttp.readyState=="loading")

{

document.getElementById("Layer2").innerHTML="loading"

}

}

function GetXmlHttpObject()

{

    var xmlHttp=null;

    try

    {

        xmlHttp=new XMLHttpRequest();

    }catch (e)

    {



        try

        {

                xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");

        } 

        catch (e) {}



    }

return xmlHttp;

}

and the get_auction page

<?php

session_start();

if (isset($_GET["cmd"]))

  $cmd = $_GET["cmd"];

else

  die("You should have a 'cmd' parameter in your URL");

$con = mysql_connect("localhost","root2","geheim");

if(!$con)

{

die('Connection failed because of' .mysql_error());

}

mysql_select_db("ebay",$con);

if($cmd=="GetAuctionData")

{

echo "<table border='1' width='100%'>

<tr>

<th>Seller ID</th>

<th>Start Date</th>

<th>Description</th>

</tr>";

$sql="SELECT SELLER_ID, ACCESSSTARTS, ARTICLE_NAME FROM Auctions";

$result = mysql_query($sql);

while ($row = mysql_fetch_array($result))

{

echo "<tr>

<td>".$row['SELLER_ID']."</td>

<td>".$row['ACCESSSTARTS']."</td>

<td>".$row['ARTICLE_NAME']."</td>

</tr>";

}

echo "</table>";

}

mysql_close($con);

?>

I am really quite stuck with this very simple thing, and would appreciate any help while I am trying to learn AJAX. many thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.