• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

jQuery AJAX Help Needed!

I currently have a verification form which takes a number input and checks to see if it is the correct number within the jquery itself.  What I would like to do is have it check against the database through an ajax call, unfortunately I have not been successful thus far.  Below is my code currently.
$("#receptVerifyForm").submit(function(e) {
                    // get user input
                    var input = $("input", this).val();
                    // do something with the answer
                    if(input=="7555"){
                            
                    }else{
                        // close the mask
                        //$.mask.close();
                        $("#submit").overlay().close();
                        showFlash("Incorrect verification code, please retry again");
                        // do not submit the form
                        return e.preventDefault();
                    }
                });

Open in new window

0
PhillipsPlastics
Asked:
PhillipsPlastics
  • 5
  • 4
1 Solution
 
Hugh McCurdyCommented:
Why are you using AJAX for this?  Why can't you just have the user submit the form and verify using PHP?  Unless you really need to verify before submitting, this looks like a lot of extra work.
0
 
PhillipsPlasticsAuthor Commented:
This is part of a much larger app so I am using a modal pop up so the user will enter in a verification code, this is what I am having the issue on and want to verify using ajax against a db.
0
 
Hugh McCurdyCommented:
I think I might have to let someone else finish this (and get the points) but I might as well advance the process by asking for some context.

Is the verification code some sort of user specific password?  If so, my guess is you'll need two way communications and I haven't done that with AJAX (and you'll need someone else).

Or is it some value that's known when the page is loaded?

What happens when the user enters a correct code?  (AJAX starts sending content?)

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PhillipsPlasticsAuthor Commented:
Okay I updated the code to reflect actual submission of the data from the form field and am trying now with the beforeSubmit ajax function.
With regard to your questions:
Currently it is a specific password of 7555 to be found in the php file which will be modified in the future but to keep things simple i have one true case and many false cases.  The problem is now not that I am receivng bad data back (it returns true or false based on form submission) but that I cannot seem to get the behaivor correct when the data comes back.  It currently always validates no matter what response comes back whether it is true or false.
$("#receptVerifyForm").submit(function(e) {
                    $.ajax({
                        url: 'checkVerificationCode.php',
                        data: 'verificationNumber='+$("input", this).val(),
                        beforeSubmit: function (data) {
                            if(data=='true'){
                                return true;
                            }else if(data == 'false'){
                                $("#submit").overlay().close();
                                showFlash("Incorrect verification code, please retry again");
                                return false;
                                //return e.preventDefault();
                            }
                        },
                        success: alert('Good')
                    });
});

Open in new window

0
 
PhillipsPlasticsAuthor Commented:
Updated and seems to be working properly but I would like some verification from the community to ensure this is the best way of doing this....
$("#receptVerifyForm").submit(function() {
                    $.ajax({
                        url: 'checkVerificationCode.php',
                        data: 'verificationNumber='+$("input", this).val(),// get user input
                        success: function (data) {
                            if(data=='true'){
                                window.location = 'contractorSignInVerified.php';
                            }else if(data == 'false'){
                                $("#submit").overlay().close();
                                showFlash("Incorrect verification code, please retry again");
                                return false;
                                //return e.preventDefault();
                            }
                        }
                    });
                    return false;
                });

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
Since you are not staying on the page if it verifies, I would put all of it in the PHP page including redirecting back to the current page on failure.  To me, you are just creating extra server traffic for no reason.
0
 
PhillipsPlasticsAuthor Commented:
If it verifies correctly it moves onto a new page (line 7), my logic for this is akin to that of an available name check verification remote call field.  If the code is incorrect it should stay on the page and cast an error note to the user.  If it verifies it will continue to the final page for signing into the facility.
0
 
Hugh McCurdyCommented:
I don't see anything wrong with it.  Further the code and intent are pretty easy to follow.

I suggest keeping this thread open for a few more days to see if you get more opinions.  I most certainly don't know everything.
0
 
PhillipsPlasticsAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 0 points for PhillipsPlastics's comment http:/Q_27385628.html#36932248

for the following reason:

Solved it myself
0
 
Hugh McCurdyCommented:
You did explain how you solved it yourself but you also asked people to review the code.  I spent time examining the code as you requested.  Perhaps that's not worth 500 points but at least a "thank you" for the code review would have been nice.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now