Sajjad Hossain
Sajjad Hossain

Reputation: 183

Ajax Not Working In Webserver

I created add cart using jQuery ajax. It's working properly in localhost (wampserver) but when I uploaded this script into a webserver then ajax function not working. it's showing an error in firebug like,

ReferenceError: cartAction is not defined

<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XH...

My ajax function is:

function cartAction(action,product_code,restaurant) {
var queryString = "";

if(restaurant != "") {

    }
if(action != "") {
var restaurant = $("#restaurant_id").val();

    switch(action) {
        case "restaurant":
            queryString = 'action=restaurant&restaurant='+ restaurant;
            // alert(queryString);
        break;
        case "add":
            queryString = 'action='+action+'&code='+ product_code+'&quantity='+$("#qty_"+product_code).val();
        break;
        case "remove":
            queryString = 'action='+action+'&code='+ product_code;
        break;
        case "empty":
            queryString = 'action='+action;
        break;
    }    
}
jQuery.ajax({
url: "http://sitename.com/ajax_action.php",
data:queryString,
type: "POST",
success:function(data){
    $("#cart-item").fadeOut(200);
    $("#cart-item").html(data);
    $("#cart-item").fadeIn(100);
    if(action != "") {
        switch(action) {
            case "add":
                $("#add_"+product_code).hide("slow");
                $("#added_"+product_code).show("slow");

            break;
            case "remove":
                $("#add_"+product_code).show("slow");
                $("#added_"+product_code).hide("slow");
            break;
            case "empty":
                $(".btnAddAction").show("slow");
                $(".btnAdded").hide("slow");
            break;
        }    
    }
},
error:function (){}
    });
}

I used this codes in my script:

<input type="number" id="qty_<?php echo $item[4]; ?>" name="quantity" class="FoodShop Quantity" value="1" size="2" />
<input type="button" id="add_<?php echo $item[4]; ?>" value="Add" class="FoodShop btnAddAction cart-action" onClick = "cartAction('add','<?php echo $item[4]; ?>')" <?php if($in_session != "0") { ?>style="display:none" <?php } ?> />
<input type="button" id="added_<?php echo $item[4]; ?>" value="Added" class="FoodShop btnAdded" <?php if($in_session != "1") { ?>style="display:none" <?php } ?> />
<input type="hidden" id="restaurant_id" value="<?php echo $ResturentID; ?>"/>

How I can solve this error?

Upvotes: 0

Views: 134

Answers (1)

Niranjan N Raju
Niranjan N Raju

Reputation: 11987

You have missed one more parameter.

<input type="button" id="add_<?php echo $item[4]; ?>" value="Add" class="FoodShop btnAddAction cart-action" onClick = "cartAction('add','<?php echo $item[4]; ?>',"")" <?php if($in_session != "0") { ?>style="display:none" <?php } ?> />
                                                                                                                                                                 ^// pass one more parameter

onClick = "cartAction('add','<?php echo $item[4]; ?>',"")"
                                                      ^ ^

Upvotes: 1

Related Questions