Reputation: 1452
I am populating a dropdown with MySQL entries using the technique in this answer. This works fine - when I select an event, a new dropdown is generated containing the dates that event is running on.
However, I also need to retrieve the cost of the event from the database, and assign it as the value of a form input. The simplest way to do it would be to assign it to the value attribute of the event dropdown, but this is already used to generate the second dropdown.
So, I figure I can use another Ajax call in the change
event of the event dropdown to query the database again and get the cost, then pass it back as a JSON encoded variable. Here are my Ajax calls:
$('#event_menu').on('change', function() {
// get selected value and build data string for AJAX
var event_selected = "event_selected="+$(this).val();
// send the selected data to a PHP page to build the populated menu
$.ajax({
url : 'populate_menu.php',
type: 'POST',
data : event_selected,
dataType : 'html',
success : function(data) {
$('#instancemenu').html(data);
}, error : function() {
alert("Something went wrong!");
}
});
$.ajax({
url : 'get_eventprice.php',
type: 'POST',
date: event_selected,
dataType : 'json',
success : function(data){
$('#totalcost').val(data);
}, error : function(){
alert("Something went wrong with the price setter!")
}
});
});
And here's the code in get_eventprice.php
:
$event_selected = isset($_POST['event_selected']) ? $_POST['event_selected'] : null;
$q="SELECT event_cost FROM events WHERE event_id=$event_selected";
$r=mysqli_query($dbc,$q);
$row=mysqli_fetch_field($r, MYSQLI_ASSOC);
echo json_encode($row['course_cost']);
However, this triggers the error clause in the Ajax call. I've also tried mysqli_fetch_array
, but with no luck. What am I doing wrong?
Upvotes: 0
Views: 132
Reputation: 11832
In your second Ajax you write date
in stead of data
edit:
Furthermore, use Element Inspector of your browser, and look in:
Upvotes: 1