Find us on facebook

Oct 9, 2014

Javascript basics

JQuery - check a radio button
$("#diversion_n").prop("checked", true);

Assign PHP value to javascript variable
var no_of_vehicles=<?php echo $no_of_vehicles;?>;

loop through auto generated fields and call ajax function

viaString="";
for (via_counter = 0; via_counter < no_of_via_points; via_counter++) {
viaid=$("#via_point_id_"+via_counter).val();
viatable=$("#via_point_table_"+via_counter).val();
viatype=$("#via_point_type_"+via_counter).val();
viapoint=$("#via_point_"+via_counter).val();
if (viaid === undefined) {
no_of_via_points =0;
//$('#no_of_via_points').val();
} else {
constidstring = "&via_point_id_"+via_counter+"=";
consttablestring = "&via_point_table_"+via_counter+"=";
consttypestring = "&via_point_type_"+via_counter+"=";
conststring = "&via_point_"+via_counter+"=";
if(viaString==""){
viaString = constidstring+ viaid+consttablestring+viatable+consttypestring+viatype+conststring+viapoint;
}else{
viaString = viaString+constidstring+viaid+consttablestring+viatable+consttypestring+viatype+conststring+viapoint;
}
}


}

var dataString = 'from_point='+ from_point + '&from_point_id=' + from_point_id + '&to_point=' + to_point+ '&to_point_id=' + to_point_id
 + '&from_point_table=' + from_point_table + '&to_point_table=' + to_point_table
 + '&from_point_type=' + from_point_type + '&to_point_type=' + to_point_type+ '&site_id=' + site_id+ '&member_id=' + member_id+ '&is_student=' + is_student
 +'&no_of_via_points='+no_of_via_points+viaString+'&optpeople='+optpeople
;
$.ajax({
 type: "POST",
 url: "vehicle_quotes.php",
 data: dataString,
 beforeSend: function(){
for(vehicle_count=1;vehicle_count<=no_of_vehicles;vehicle_count++){
jQuery("#ajax_loader_quote_"+vehicle_count).show();
jQuery("#quote_price_"+vehicle_count).hide();
jQuery("#ajax_loader_quote_member_"+vehicle_count).show();
jQuery("#quote_price_member_"+vehicle_count).hide();
}
},
 complete: function(){
for(vehicle_count=1;vehicle_count<=no_of_vehicles;vehicle_count++){
jQuery("#ajax_loader_quote_"+vehicle_count).hide();
jQuery("#quote_price_"+vehicle_count).show();
jQuery("#ajax_loader_quote_member_"+vehicle_count).hide();
jQuery("#quote_price_member_"+vehicle_count).show();
}
},
 success: function(response)
 {
var priceObject = JSON.parse(response);
length=priceObject.price.length;
for(vehicle_count=0;vehicle_count<length;vehicle_count++){
vehicle_id = priceObject.price[vehicle_count].vehicle_id;
$('#price_'+vehicle_id).html("\u00A3"+priceObject.price[vehicle_count].journey_price_one_way);
$('#price_member_'+vehicle_id).html("Member Price: \u00A3"+priceObject.price[vehicle_count].journey_price_one_way_member);
}
 }
});

No comments:

Post a Comment