?
Solved

jquery .ajax() not executing url paramater

Posted on 2012-04-05
6
Medium Priority
?
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 2000 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
Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

 
LVL 20

Assisted Solution

by:Proculopsis
Proculopsis earned 2000 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

Industry Leaders: 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!

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
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)
Suggested Courses

719 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