Solved

jquery .ajax() not executing url paramater

Posted on 2012-04-05
6
412 Views
Last Modified: 2012-04-18
Hi,

I have this bit of code to check if a username and/or email already exists in the database:
/**************************
* Check if usernamer or email exists in db already.
**************************/
function checkUsernameEmail(username, email) {
    $.ajax({
        type : 'POST',
        url: './model/modCheckUsernameEmail.php',
        dataType: 'json',
        data: {
            username : username,
            email : email
        },
        success: function(retData, textStatus, jqXHR) {
            if (retData.success==1) { //means does not username and/or email exits in database already
                return true;
            } else { //data.success==0 (i.e. username and/or email already exists in database)
                if (retData.username != '' || retData.username != null) {$('#usernameerror').text('username already exists in database').show(500);}  
                if (retData.email != '' || retData.email != null) {$('#emailerror').text('email already exists in database').show(500);}
                return false;
            }
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
        }
    });
}

Open in new window


It works fine on the first run. For instance, if I have a username and/or email in the database then it returns the error. But if I change the username and then click on the submit button again. The url parameter in the .ajax() is not executed. This line:
url: './model/modCheckUsernameEmail.php',

I can tell because my PHP debugger does not get stepped through to this file. Oddly, when I'm stepping through with firebug on the second submit with a different username I can see the variable is passed with this new username in this section:
data: {
            username : username,
            email : email
        },

But the url parameter is not called. Why is that and how can I work around this issue?

Thanks,
Victor
0
Comment
Question by:Victor Kimura
  • 3
  • 2
6 Comments
 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 500 total points
ID: 37812550

Could it be a cache problem, try adding the follwing:

cache: false
0
 
LVL 14

Expert Comment

by:binaryevo
ID: 37812574
I was thinking the same thing as Proculopsis...  Do as suggested and see what happens.
0
 

Author Comment

by:Victor Kimura
ID: 37812610
It doesn't work. =(

Any think else?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 500 total points
ID: 37813318
Just to make sure, add the following to the data:

random: new Date().getTime()
0
 

Accepted Solution

by:
Victor Kimura earned 0 total points
ID: 37845335
Hi fellas, I think what was happening is that I was calling checkUsernameEmail() from another .ajax({.success: function() {...}}); call and the initial calling function was not returning any value. So on the calling function, say, callingFunction() {.ajax({...}) I would call checkUsernameEmail() on success from within callingFunction() and this works. I think what was tripping me was that .ajax does not return any thing like:
var retVar = callingFunction() {.ajax({...})
doesn't work because retVar is null or ''.

Thank you for your input and help.
0
 

Author Closing Comment

by:Victor Kimura
ID: 37859798
Please read my last comment. =)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is a Lightbox? A Lightbox is the effect you see when you click, for example, an image and the screen fades out and up pops the same image but in its full size dimensions. There are lots of Lightbox effects for jQuery. Problem is they are a…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now