Solved

jquery .ajax() not executing url paramater

Posted on 2012-04-05
6
398 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now