Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jquery .ajax() not executing url paramater

Posted on 2012-04-05
6
Medium Priority
?
481 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

916 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