Jump to content

Changing variables and reloading sql query with Java Script/Jquery


AlenD

Recommended Posts

Hello, I have a div that contains echos from mySQL. I have another div that echos the titles of the contents of the table.

 

I want to change the contents of the div depending on which title I click. I have placed unique IDs for the titles, so I was wondering how I could change the content of the div. :)

By the way, my code looks like this: Basically I want to change the display of the echos of query 2 when I click query 1. :)

 

<?php include "backend/settings.php"; mysql_connect ("localhost", $username, $password);
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>

<script type="text/javascript" src="scripts/jquery-1.6.2.min.js"></script>

<script>
$(document).ready(function(){
	$("#atdisp").click(function(){
				$("").html(?);					  
	});			
});
</script>

</head>

<body>

<?php
    $query = "SELECT * FROM activities";
$result = mysql_query($query);
$num = mysql_numrows($result);
$id = mysql_result($result, 0, "id");

$i = 0;

while ($i < $num) {
	$title = mysql_result($result, $i, "title");

	echo '<div id="atdisp" class="atdisp2"><a href="#">'.$title.'</a></div><div class="spacer"></div>';
	$i++;
}
?>
            
<?php
$query2 = "SELECT * FROM activities WHERE id = $id";
$result2 = mysql_query($query2);

$title = mysql_result($result2, 0, "title");
$date = mysql_result($result2, 0, "date");
$machinery = mysql_result($result2, 0, "machinery");
$info = mysql_result($result2, 0, "info");
$image = mysql_result($result2, 0, "image");

echo '<div id="cdisp">';
echo '<div id="tdisp">'.$title.'</div><div class="spacer"></div>';

echo '<div id="ddisp">'.$date.'</div><div class="spacer"></div>';

echo '<div id="idisp">'.$info.'</div><div class="spacer"></div>';

$list = explode(",",$machinery);
foreach($list as $machinery){
    	echo '<div id="mdisp"><li>'.$machinery.'</li></div>';
} 

echo '<div id="pdisp"><img src="images/'.$image.'" /></div>';
echo '</div>';

mysql_close();

?>

</body>
</html>

Since you are using jQuery, you might want to start at http://api.jquery.com/category/ajax/ (review this part as well http://api.jquery.com/jQuery.get/ for getting data using ajax). Lots of examples to follow

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.