Im trying to get this code to work. I borowed it from demodirectory.com just to try and learn how it works.
Its for listings. Theres a price for different levels of listings, say 1 free category then x$$ for each extra.
It uses AJAX?? to process then shows the total price in a div.
This is the JS code:
function orderCalculate() {
var xmlhttp;
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xmlhttp = false;
}
}
}
if (document.getElementById("check_out_payment")) document.getElementById("check_out_payment").className = "isHidden";
if (document.getElementById("check_out_free")) document.getElementById("check_out_free").className = "isHidden";
if (document.getElementById("loadingOrderCalculate")) document.getElementById("loadingOrderCalculate").style.display = "";
if (document.getElementById("loadingOrderCalculate")) document.getElementById("loadingOrderCalculate").innerHTML = "Wait, Loading...";
if (xmlhttp) {
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var price = xmlhttp.responseText;
if (price > 0) {
if (document.getElementById("check_out_payment")) document.getElementById("check_out_payment").className = "isShown";
if (document.getElementById("checkoutpayment_total")) document.getElementById("checkoutpayment_total").innerHTML = "<strong>Total Price Amount: </strong>£"+price.substring(0, price.length-2)+"."+price.substring(price.length-2, price.length);
} else {
if (document.getElementById("check_out_free")) document.getElementById("check_out_free").className = "isShown";
if (document.getElementById("checkoutfree_total")) document.getElementById("checkoutfree_total").innerHTML = "<strong>Total Price Amount: </strong>$FREE";
}
if (document.getElementById("loadingOrderCalculate")) document.getElementById("loadingOrderCalculate").style.display = "none";
if (document.getElementById("loadingOrderCalculate")) document.getElementById("loadingOrderCalculate").innerHTML = "";
}
}
}
var get_level = 0;
if (document.order_listing.level) get_level = document.order_listing.level.value;
var get_categories = 0;
if (document.order_listing.feed) get_categories = document.order_listing.feed.length;
var get_discount_id = "";
if (document.order_listing.discount_id) get_discount_id = document.order_listing.discount_id.value;
xmlhttp.open("GET", "http://testsite.com/ordercalculate.php?level="+get_level+"&categories="+get_categories+"&discount_id="+get_discount_id, true);
xmlhttp.send(null);
}
}
function levelSwitch(level) {
var extracat_note_4 = 'Categories <strong>FREE: 3</strong>. Extra categories/sub-categories cost an <strong>additional $ 20.00</strong> each. Be seen!';
var extracat_note_3 = 'Categories <strong>FREE: 2</strong>. Extra categories/sub-categories cost an <strong>additional $ 15.00</strong> each. Be seen!';
var extracat_note_2 = 'Category <strong>FREE: 1</strong>. Extra categories/sub-categories cost an <strong>additional $ 10.00</strong> each. Be seen!';
var extracat_note_1 = 'Category <strong>FREE: 1</strong>. Extra categories/sub-categories cost an <strong>additional $ 5.00</strong> each. Be seen!';
document.order_listing.level.value = level;
if (document.getElementById("extracategory_note")) document.getElementById("extracategory_note").innerHTML = eval("extracat_note_" + level);
orderCalculate();
}
function JS_addCategory(text, id) {
seed = document.order_listing.seed;
feed = document.order_listing.feed;
var flag=true;
for (i=0;i<feed.length;i++) if (feed.options[i].value==id) flag=false;
if (text && id && flag) {
orderCalculate();
feed.options[feed.length] = new Option(text, id);
alert('Category "'+text+'" successfully added!');
} else {
if (!flag) alert('This category was already inserted');
else alert('Please, select a valid category');
}
}
function JS_removeCategory() {
feed = document.order_listing.feed;
if (feed.selectedIndex >= 0) {
feed.remove(feed.selectedIndex);
orderCalculate();
}
}
function JS_submit() {
feed = document.order_listing.feed;
return_categories = document.order_listing.return_categories;
if(return_categories.value.length > 0) return_categories.value = "";
for (i=0;i<feed.length;i++) {
if (!isNaN(feed.options[i].value)) {
if (return_categories.value.length > 0) return_categories.value = return_categories.value + "," + feed.options[i].value;
else return_categories.value = return_categories.value + feed.options[i].value;
}
}
}
Then this is the code that is in the ordercalculate.php page:
if($_GET['level'] = "4"){
$listing_price = "29999";
} elseif ($_GET['level'] = "3"){
$free = "3";
$extra = "1500";
$listing_price = "19999";
} elseif ($_GET['level'] = "2"){
$free = "1";
$extra = "1000";
$listing_price = "6000";
} elseif ($_GET['level'] = "1"){
$free = "1";
$extra = "500";
$listing_price = "0";
}
$cat_total = $_GET['categories'];
if($cat_total > $free){
$cat_price = $extra*($cat_total-$free);
}
echo $listing_price+$cat_price;
It kinda works, but when adding the categories its not calculating right... if at all. The ordercalculate.php page is just something i threw together to figure out how it works.
If i go to the ordercalculate page directly and put: ordercalculate.php?level=2&categories=3 it will display the listing price with 2 free cats + 10.00 for the extra one. But it wont do it on the page the code is in.
Any suggestions??
Danny