mercredi 9 septembre 2015

Possible to change the $.post url based on an if statement?

I apologize in advance for my ignorance, I am still really new to jquery and php. If I am trying to do something horrid then please let me know. That being said, here is what I am doing:

The following script is working properly, its just a small snippet from my 'createLogin.js'.

Throughout the script there are numerous calls to php files like this:

$.post( 'verifyUserNo.php' ,  //this is url is what I want to change
{
    No   : trimmedUserNo ,
    Name : companyName

} ).done( function( verifyUserNo ) {

            if ( verifyUserNo  == "Not a valid customer name or code" ) {

                console.log( "The Company Code Entered Is Not Authentic" ) ;

            } else
            if ( verifyUserNo == "valid" ) {

                console.log( "The Company Code Entered Is Authentic" ) ;


             $.post( 'compareUserName.php' ,   //This post will be the same 
                                               //regardless of user level.
             {
                 newName : desiredName

             } ).done( function( data ) {

                 if (data == "taken") { alert('Choose another username') ;}
                 else if (data == "available") {

                     $.post( 'removeUserNo.php'   //This one needs
                                                  //to change as well

Though its working as it is, I am adding a condition where the type of user being created is calculated by a number pulled from my database earlier in the script. i.e.

var usrLevel = 2  //could is dynamically set to any int greater than 0 and less than 5.

Now, if usrLevel == 2 then the script above is what i want to execute. However,

if usrLevel == X //where x is not 2 I want to change

$.post( 'verifyUserNo.php' to $.post( 'verifyUserAth.php' because the data for users with a level or 3 is stored in a separate table on my db than if their level is 2.

Without nesting the rest of my script inside an if/else statement. I am trying to change the above url to direct to a different php file for each value of usrLevel so essentially 4 possibilities.

Aucun commentaire:

Enregistrer un commentaire