Onsubmit not working inside ajax function

Hi

I need to check if values exist on the database before I can submit form to paypal, The problem is im using ajax to do that and since ajax is asynchronous programming, its messing up with the form onsubmit function. I have tried several ways and have failed in all of them.

This is one of the latest version im trying to work on

function ajax_paypal(orders) {
        var form_paypal = document.getElementById("pp1");
        var htpr = new XMLHttpRequest();
        var url = "testtp";
        var val = "orders=" + orders;
        htpr.open("POST", url, true);
        htpr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        htpr.onreadystatechange = function () {
            if (htpr.readyState == 4 && htpr.status == 200) {
                var sold_out_ids = htpr.responseText;
                if (sold_out_ids > 0) {
                    alert("One of your items has sold out! Sorry for any inconvenience");
                } else {
                    form_paypal.submit();
                }

            }
        };
    }
<form  onclick="is_available()" onsubmit="event.preventDefault()" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" id="pp1">

If form_paypal.submit is outside the ifs statements it will work properlly but because is inside it doesnt work. Yes ifs are working fine as i tested before.

How you triger ajax_paypal?

This wouldn’t make any difference. You’ll have to actually .send() the request at some point though in order to get inside any of those if branches, as well as of course call/bind ajax_paypal somewhere as @fumeeptc noted .

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.