2

I have to call a function only if ajax call success.

Code:

<script>
    function removeFromCart(id) {
        var value = "";
        for(var i=0; i<id.length; i++)
            if(!isNaN(id[i]))
                value += id[i];
        $.ajax({
            type: "POST",
            url: "http://url.com/removefromcart.php",
            data: "id="+value,
            success: function(response){
                afterRemove(response);
            }
        });
    }

    function afterRemove(response) {
        if(response[0] == "r") {
            var prezzo = "";
            for(var i = 1; i<respose.length; i++)
                var prezzo += response[i];
            document.getElementByid("prezzo").innerHTML = prezzo+".00 &euro;";
            $("#segnale"+value).fadeOut();
        }
    }
</script>

The problem is that Firebug gives me this error on page load:

invalid variable initialization: var prezzo += response[i];

This because the afterRemove function is called on page load and not only if ajax success. How can fix this?

2
  • 1
    You wrote respose instead of response in your for initialization. Commented Nov 11, 2011 at 15:28
  • Just FYI, be consistent; don't mix jQuery ($("#segnale"+value)) lookups and non-jQuery lookups (document.getElementByid("prezzo")). $("#prezzo").html(prezzo+".00 &euro;"); Commented Nov 11, 2011 at 15:35

4 Answers 4

6

you define/var the variable prezzo twice

var prezzo = ""; // <-- here
for(var i = 1; i<respose.length; i++)
    var prezzo += response[i]; // <-- and here
Sign up to request clarification or add additional context in comments.

Comments

2
var prezzo += response[i]

This is invalid because var prezzo means you are creating a new variable named prezzo. You are then trying to add to that variable. You can't add to a variable that is brand new because it doesn't have a value yet. Just drop the var.

Comments

1
var prezzo += response[i];

This line makes no sense. a += b means a = a+b.

In your case, you're doing var prezzo += response[i]; which is var prezzo = prezzo + response[i];. prezzo doesn't exist yet (because you are declaring it right there), so you can't add to its value, because it doesn't have a value (yet).

(Also, it's invalid syntax)

EDIT: I noticed you declared var prezzo = ""; earlier, so just remove the var:

prezzo += response[i];

It should look like:

var prezzo = "";
   for(var i = 1; i<response.length; i++)
      prezzo += response[i];

Comments

1

if you initialize the variable when you do ++. you will always get error...

function afterRemove(response) {
    if(response[0] == "r") {
        var prezzo = "";
        for(var i = 1; i<respose.length; i++)
             prezzo += response[i];
        document.getElementByid("prezzo").innerHTML = prezzo+".00 &euro;";
        $("#segnale"+value).fadeOut();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.