Hi folks,
I’m having difficulty getting jquery to create dynamic html hyperlinks using a PHP and MySQL database backend. Each row returned from the mysql db should also have a hyperlink that allows the user to click on the row and view it (e.g. item.php?id=18)
I AM ABLE to get jquery to output regular text (no hyperlink) using the php and mysql code below, but no hyperlink.
I’m using 2 php files to do this:
//1st php file called jqueryapi.php:
<?php
include ‘includes/common.inc.php’; //provides login credentials to the MySQL db
$result = mysql_query(“SELECT title, minimum_bid, id FROM auctions WHERE auctions.closed = 0”); //pulls auction info from db
$array = array();
$row1 = mysql_fetch_row($result);
while($row1) {
$array[] = $row1;
$row1 = mysql_fetch_row($result);
}
echo json_encode($array); //outputs the code into json
?>
//2nd php file called jqueryclient.php
<?php
//php heredoc code below to output to text
echo <<<HTEXT
<html>
<head>
<script language=”javascript” type=”text/javascript” src=”js/jquery.js”></script>
</head>
<body>
<!————————————————————————-
1) Create some html content that can be accessed by jquery
————————————————————————–>
<h2> We recommend These Auctions: </h2>
<div id=”output”></div>
//this is not working
$(“#output”).html(“<a href=”http://google.com”>A link to google</a>”);
<script id=”source” language=”javascript” type=”text/javascript”>
$(function ()
{
//———————————————————————–
// 2) Send a http request with AJAX [url]http://api.jquery.com/jQuery.ajax/
//———————————————————————–
$.ajax({
url: ‘jqueryapi.php’, //the script to call to get data
data: “” //you can insert url argumnets here to pass to jqueryapi.php
//for example “id=5&parent=6”
dataType: ‘json’, //data format
success: function(data) //on recieve of reply
{
var cid = data[0]; //get id
var cname = data[1]; //get name
var hyper = $(this).attr(‘href’); //try to get hyperlink from returned database results, this is also not working
//——————————————————————–
// 3) Update html content
//——————————————————————–
for(var i = 0; i < data.length; i++)
{
$(‘#output’).append(“<b>Title: </b>”+data[i][0]+”<b> Price: $</b>”+data[i][1]); //outputting regular text but no hyperlink.
$(‘#output’).append(“<br>”);
}
}
});
});
</script>
</body>
</html>
HTEXT;
?>
I’ve tried different selectors and also tried using .append in jquery but keep getting errors. I’ve done a lot of research about this on the internet but can’t find a satisfactory solution. If somebody could provide the working jquery code for hyperlinks tag, it would be great. Thanks so much for your help!
Peter