jbcedge
jbcedge

Reputation: 19505

how to disable submit button with ajax based on ajax response

I am trying to use something like this in my code but having no luck...

function captcha(data) {
    var httpxml;
    try {
        // Firefox, Opera 8.0+, Safari
        httpxml = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            httpxml = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                httpxml = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }

    function stateck() {
        if (httpxml.readyState == 4) {
            document.getElementById("cap_error").innerHTML = httpxml.responseText;
            var result = httpxml.responseText;
            if (result == 'Error..try again') {
                document.getElementById("regSubmit").disabled = true;
            }


        }
    }
    var url = "includes/captcha.php";
    url = url + "?datacap=" + data;
    url = url + "&sid=" + Math.random();
    httpxml.onreadystatechange = stateck;
    httpxml.open("GET", url, true);
    httpxml.send(null);
}

Edit:

My submit button is:

<input type="submit"  value="Submit" id="regSubmit" name="Submit"/>  

When ajax function returns 'Error..try again' regSubmit button should be disabled. But this function is not working for my right now.

if (result == 'Error..try again')
{
  document.getElementById("regSubmit").disabled = true;
}

Upvotes: 0

Views: 1269

Answers (1)

Andrew
Andrew

Reputation: 674

This is what we have in our app:

document.getElementById('buttonId').disabled=true;

And it works.

Two things:

1 - Make should you clear your browser cache after you change your javascript.

2 - Put an alert before you disable your button to make sure you are getting into your 'if' statement.

Upvotes: 1

Related Questions