Avatar of PhillipsPlastics
PhillipsPlastics

asked on 

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

AJAXJavaScriptPHP

Avatar of undefined
Last Comment
Hugh McCurdy
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

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.
Avatar of PhillipsPlastics
PhillipsPlastics

ASKER

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.
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

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?)

Avatar of PhillipsPlastics

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of PhillipsPlastics
PhillipsPlastics

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

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.
Avatar of PhillipsPlastics

ASKER

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.
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

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.
Avatar of PhillipsPlastics

ASKER

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
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

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.
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo