Reputation: 3628
Please see website.
My database has a table with values 'name' (the event under step 2 on the left), 'price', 'date' etc and I'd like to display them dynamically in the dark box on the right depending on which event is chosen.
I'm currently displaying the event itself with the code below, but I'm unsure as to how to develop it to grab database values based on this choice. I'm guessing some sort of .get().
<script type="text/javascript">
$(document).ready(function() {
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>');
});
$('#club').change(function(event) {
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>');
});
</script>
This has had me stumped for ages, so any help would be much, much appreciated!
EDIT
Thanks for your replies. Here's what I now have, but it isn't working.
jQuery:
<script type="text/javascript">
$(document).ready(function() {
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>');
});
$('#club').change(function(event) {
$.ajax({
type: "post",
url: "eventinfo.php",
data: $(this).serialize(),
success: function(data) {
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>Price'+data.price);
},
dataType: "json"
});
});
</script>
eventinfo.php:
<?php
// the name of the input text box is 'club'
$night = $_POST['club'];
$night = mysql_real_escape_string($night);
// one of the columns values included in * is 'price'
// this line was previously: $query = mysql_query("SELECT * FROM nights WHERE name = '$night'");
$query = "SELECT * FROM nights WHERE name = '$night'";
$result = mysql_query($query);
$items = array();
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$items[] = array($row[0]);
}
}
mysql_close();
// convert into JSON format and print
echo json_encode($items);
?>
Upvotes: 1
Views: 1174
Reputation: 3298
If I understand you right, you want an AJAX call to get the price. So something like
$('#club').change(function(event) {
$.ajax(
{type: "post",
url: "/path/to/price",
data: $(this).serialize(),
success: function(data)
{
$('#right_inside').html('<h2>' + $('#club').val() + '</h2>Price'+data.price);
},
dataType: "json"
});
Then since you are using PHP get the values you want and load them into an array and use json_encode.
For the PHP try changing
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$items[] = array($row[0]);
}
}
to
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$items[] = array("price"=>$row['price']);
}
}
Try adding the following to your PHP file, near the top:
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
Upvotes: 1
Reputation: 13549
This is a good place to use client side templating. Use jQuery.tmpl and make a template for the right side (example below):
<script id="infoTemplate" type="text/html">
<div>
<span>${price}</span>
<span>${information}</span>
</div>
</script>
Then, make a method in PHP that takes in something like eventId and passes back a JSON object that looks something like this: {"price":"123.34","information":"some info here"}. To load up your template, do this:
$(document).ready(function(){
// ... other stuff
$.template("infoTemplate", $("#infoTemplate").html());
});
Then, in your change event handler, do this:
$('#club').change(function(event) {
$.get('/yourNewPHPMethod', $('#club').val(), function(data){
$('#right_inside').html($.tmpl('infoTemplate', data));
});
});
Sorry, didn't have time to test any of this but it's the main idea and should help you establish a good pattern for any of this type of work in the future. See jQuery.tmpl documentation below: http://api.jquery.com/jquery.tmpl/
Upvotes: 1
Reputation: 906
The best course of action would be to use an xmlhttp object to load your PHP page that echos the data out. From that xmlhttp object you can assign the responseText (which will be the output contents of your PHP page) to a Javascript variable.
In other words, you probably want to use AJAX.
Upvotes: 1