Why i can't pass the variable co2 to jquery function? nothing shows up where's wrong ? i want to use co2 variable in my jquery function.
<?php
...
$co2 = (($miles / 41.986) * 20.88 * 2.7) / 2204.6;
echo json_encode(array('co2' => $co2));
?>
jquery:
$("#aircalc").click(function(){
var dept = $("#dept").val();
var dest = $("#dest").val();
$.post('airtravel.php',{dept: dept, dest: dest}, function(data){
var a = data.co2;
$("#airanswer").html(a);
});
});
Try this code.
$("#aircalc").click(function(){
var dept = $("#dept").val();
var dest = $("#dest").val();
values: [ <?php echo $co2; ?>],
$.post('airtravel.php',{dept: dept, dest: dest}, function(data){
var a = data.values[0];
$("#airanswer").html(a);
});
});
you can not access co2
because you are calling regular post ajax and return json
object you need to call ajax request with json type something like this
$.ajax({
type: "POST",
dataType: "json"
url: 'airtravel.php',
data: {dept: dept, dest: dest}
})
.done(function( data ) {
var a = data.co2;
$("#airanswer").html(a);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.