Solved

jquery .ajax() not executing url paramater

Posted on 2012-04-05
6
422 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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)
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…

770 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