3
function coupon_check_plan() {
   if (document.getElementById('r5').checked) {
        var ret = false;
        var coupon = $("#coupon").val();
        if (coupon == "") {
            ret = false
            $("#er3").html('<img src="' + img_loc + 'cross.png"/> Cannot be left blank');
        }
        if (coupon != "") {
            $.post("coupon_check.php", {
                    "coupon": coupon
            }, function (data) {
            if (data.result == "0") {
                ret = false;
                $("#er3").html('<img src="' + img_loc + 'cross.png"/> This Code is aready used');
            }
            if (data.result == "-1") {
                ret = false;
                $("#er3").html('<img src="' + img_loc + 'cross.png"/> This Code is invalid');
            }
            if (data.result == "1") {
                $("#frmsup1").submit();
            }
        });
        }
    } else {

    }
    return ret;
}

<form action="sbs_check2.php" method="POST" id="frmsup1" onsubmit="return coupon_check_plan();" >
    <input type="radio" name="package" value="Coupon" id="r5" checked="true" onclick="choose()" />
    <input type="text"  name="coupon" id="coupon" onfocus="document.getElementById('er3').innerHTML='';" placeholder="Enter Coupon Code" style="margin-top:10px;"/>
    <div class="err" id="er3"></div>
    <input type="radio" name="package" value="Free" id="r1" onclick="choose()" />
    <input type="radio" name="package" value="bronze" id="r2" onclick="choose()" />
    <input type="submit" name="submit" value="Register Now" class="submitbut2" id="pur">
</form>

When I click on the submit button, if the the radio button with id r5 is clicked then with id coupon is send an ajax request to check wether the coupon is valid or not and if ajax result give a 1 then it will submit the form but like a loop it is sending the request and the result is 1 only but it is not submittng the form.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
PHP_USER1
  • 628
  • 1
  • 14
  • 29
  • Have you tried `alert(data)` to see what is returned? – Javad Mar 09 '14 at 06:16
  • yes i have tried this – PHP_USER1 Mar 09 '14 at 06:22
  • I believe `onsubmit="return coupon_check_plan();"` is where you're wrong. `coupon_check_plan()` is PHP function so web browser won't be able to call it. Additionally I'd suggest using `$("form#frmsup1").submit(function(){//code here});` despite of onsubmit argumet. – Michał Mar 09 '14 at 06:25
  • @Michał What are you talking about? `coupon_check_plan` is a Javascript function. – Barmar Mar 09 '14 at 06:25
  • 1
    Ajax is async so you cannot use the return statement. You need to ALWAYS return false if you want to do ajax in the onsubmit – mplungjan Mar 09 '14 at 06:26
  • yes Its a java script function – PHP_USER1 Mar 09 '14 at 06:26
  • Could you tell me what was the output of `alert(data)` or `alert(data.result)`? You can also check the returned value for Ajax in Dev mode of browser or some tools like Fiddler – Javad Mar 09 '14 at 06:37
  • I have checked in console , alert – PHP_USER1 Mar 09 '14 at 06:41

1 Answers1

0

Try this:

function coupon_check_plan() {
   if (document.getElementById('r5').checked) {
        var coupon = $("#coupon").val();
        if (coupon == "") {
            $("#er3").html('<img src="' + img_loc + 'cross.png"/> Cannot be left blank');
        }
        else {
          $.post("coupon_check.php", {
                "coupon": coupon
            }, function (data) {
            if (data.result == "0") {
                $("#er3").html('<img src="' + img_loc + 'cross.png"/> This Code is aready used');
            }
            if (data.result == "-1") {
                $("#er3").html('<img src="' + img_loc + 'cross.png"/> This Code is invalid');
            }
            if (data.result == "1") {
                $("#frmsup1").submit();
            }
          });
        }
    }
    return false;
}

It is also recommended to use switch(data.result) instead of if(data.result).

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55