Jump to content

Changing variables and reloading sql query with Java Script/Jquery


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.