+ Reply to Thread
Results 1 to 10 of 10

Thread: Ajax Login ajaxForm() issues

  1. #1
    pythondev is offline x10Hosting Member pythondev is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    59

    Ajax Login ajaxForm() issues

    I will paste a bt of code to show how things are working first then explain the problem...
    Javascript (jQuery):
    Code:
    $(document).ready(function()
    {
    $('#loginf').ajaxForm({
                beforeSubmit: function () { $('#getLog').html('<img src="/img/ajax-loader.gif">')},
                target: '#getLog'
            });
    
    $('#logOut').ajaxForm({
                beforeSubmit: function () { $('#getLog').html('<img src="/img/ajax-loader.gif">')},
                target: '#getLog'
            });
    
    });
    PHP
    PHP Code:
    $s_doLogin $this->loginDisp();

            if (isset(
    $_POST['ajaxCall']) && $_POST['ajaxCall'] == '1'){
            echo 
    $s_doLogin;
            die;
            }

    [
    B]public function loginDisp[/B](){
            if (
    $this->obj_protect->showPage){
            
    $curUser=$this->obj_protect->getUser();
            
    $s_doLogin "<span id=\"userLog\">Welcome<br>".$curUser."</span><br><br>\r\n";
            
    $s_register $this->obj_language->get("language/ucPanel");
            
    $s_doLogin .= "<a href=\"/cp/\">".$s_register."</a><br>\r\n";
            
            if (
    $this->obj_protect->checkAdmin() == "1"){
            
    $s_doLogin .= "<a href=\"/adm/\">Admin</a><br>\r\n";
            }
            
    $s_doLogin .= $this->obj_layouts->doLogout();
            
            } else {
            
            
    $s_register $this->obj_language->get("language/register");
            
    $s_user $this->obj_language->get("language/login/username");
            
    $s_pass $this->obj_language->get("language/login/pass");
            
    $s_remMe $this->obj_language->get("language/logRem");

            
    $s_doLogin $this->obj_layouts->doLogin($s_user$s_pass$s_remMe);
            
    $curUser=$this->obj_protect->checkLogin();
            
    $s_errMsg $this->obj_language->get("language/userPass");
            
    $s_curUserstr_replace("{userPass}"$s_errMsg ,$curUser);
            
    $s_doLogin .= $s_curUser;
            
    $s_doLogin .= "<br><a href=\"/index.php?p=register\">$s_register</a>";
            

            
            }
            return 
    $s_doLogin;

    PHP:
    PHP Code:
    public function doLogin($s_user$s_pass$s_remember="Remember"){
        
    $getURI $_SERVER['REQUEST_URI'];
        
    //"<form id=\"login\" name=\"login\" method=\"post\" action=\"".$getURI."\">
        
    $s_return "<form id=\"loginf\" method=\"post\" action=\"".$getURI."\" >

          <label>"
    .$s_user."
            <input type=\"text\" name=\"username\" id=\"username\" title=\"required\" size=\"16\" />
          </label>
          <br />
                  <label>"
    .$s_pass."
              <input type=\"password\" name=\"password\" id=\"password\" title=\"required\" size=\"16\"/>
            </label>
            <input type=\"hidden\"  id=\"action\" name=\"action\" value=\"login\" />
            <input type=\"hidden\" id=\"ajaxCall\" name=\"ajaxCall\" value=\"1\" />
            <input name=\"login\" type=\"submit\" id=\"login\" value=\"Login\"/><br>
        "
    .$s_remember."<input type=\"checkbox\" id=\"userRemember\" name=\"userRemember\" value=\"yes\">
           </form>"
    ;
        return 
    $s_return;
        }

        public function 
    doLogout(){
        
    $getURI $_SERVER['REQUEST_URI'];
        
    $s_return "<form id=\"logOut\" method=\"post\" action=\"".$getURI."\">
        <input type=\"hidden\" id=\"ajaxCall\" name=\"ajaxCall\" value=\"1\" />
            <input type=\"hidden\" id=\"action\" name=\"action\" value=\"logout\" />
            <input name=\"logout\" type=\"submit\" id=\"logout\" value=\"Log Out\" /><br>
          </form>"
    ;
        return 
    $s_return;
        } 
    Now the Issue:

    the whole thing works fine as long as you click a link after you login and reload the page, but if you login, then logout straight away it throws you to the textResponse page instead of loading into the getLog division.

    any idea's on how to counteract this and load the txtResponse into the right place?
    Last edited by pythondev; 10-24-2009 at 11:58 PM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Ajax Login ajaxForm() issues

    Quote Originally Posted by pythondev View Post
    Code:
    $(document).ready(function()
    {
    $('#loginf').ajaxForm({
                beforeSubmit: function () { $('#getLog').html('<img src="/img/ajax-loader.gif">')},
                target: '#getLog'
            });
    
    $('#logOut').ajaxForm({
                beforeSubmit: function () { $('#getLog').html('<img src="/img/ajax-loader.gif">')},
                target: '#getLog'
            });
    
    });
    [...] if you login, then logout straight away it throws you to the textResponse page instead of loading into the getLog division.
    When the page loads, #logOut doesn't exist, so the call to $('#logOut').ajaxForm() fails.

    Quote Originally Posted by pythondev View Post
    PHP Code:
    public function doLogin($s_user$s_pass$s_remember="Remember"){
        
    $getURI $_SERVER['REQUEST_URI'];
        
    //"<form id=\"login\" name=\"login\" method=\"post\" action=\"".$getURI."\">
        
    $s_return "<form id=\"loginf\" method=\"post\" action=\"".$getURI."\" > 
    Why use double quoted strings when you're not using variable interpolation? Why use double quotes for HTML attributes in a double quoted string, which requires escaping the quotes, when you can use single quotes?

    There are three alternatives, each of which is less messy.
    • Single quote the PHP string, double quote the attributes, concatenate the variables:
      PHP Code:
          $s_return '<form id="loginf" method="post" action="' $getURI '" >
      ...'

    • Double quote the PHP string, single quote the attributes, rely on interpolation:
      PHP Code:
          $s_return "<form id='loginf' method='post' action='$getURI' >
      ... 
    • (my recommendation for this case) use heredoc strings:
      PHP Code:
          $s_return = <<<EOS
      <form id="loginf" method="post" action="$getURI" >
      ...
      EOS; 
      Note you can use any word to end the heredoc string. I used "EOS" for "End Of String", but "EOF" and "ETB" (from the End of Transmission Block control character) are also common.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    pythondev is offline x10Hosting Member pythondev is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    59

    Re: Ajax Login ajaxForm() issues

    Quote Originally Posted by misson View Post
    When the page loads, #logOut doesn't exist, so the call to $('#logOut').ajaxForm() fails.


    Why use double quoted strings when you're not using variable interpolation? Why use double quotes for HTML attributes in a double quoted string, which requires escaping the quotes, when you can use single quotes?

    There are three alternatives, each of which is less messy.
    • Single quote the PHP string, double quote the attributes, concatenate the variables:
      PHP Code:
          $s_return '<form id="loginf" method="post" action="' $getURI '" >
      ...'

    • Double quote the PHP string, single quote the attributes, rely on interpolation:
      PHP Code:
          $s_return "<form id='loginf' method='post' action='$getURI' >
      ... 
    • (my recommendation for this case) use heredoc strings:
      PHP Code:
          $s_return = <<<EOS
      <form id="loginf" method="post" action="$getURI" >
      ...
      EOS; 
      Note you can use any word to end the heredoc string. I used "EOS" for "End Of String", but "EOF" and "ETB" (from the End of Transmission Block control character) are also common.
    After some playing around, I found, my php looks nice, doesn't change it's effect.. but does nothing to deal with the AJAX issue.
    any advice on that ?

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Ajax Login ajaxForm() issues

    Quote Originally Posted by pythondev View Post
    After some playing around, I found, my php looks nice, doesn't change it's effect..
    The string format was a matter of readability.

    Quote Originally Posted by pythondev View Post
    but does nothing to deal with the AJAX issue. any advice on that ?
    Did you work on the problem with #logOut not existing when the argument to $(document).ready(...) is invoked?


    Quote Originally Posted by misson View Post
    When the page loads, #logOut doesn't exist, so the call to $('#logOut').ajaxForm() fails.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    pythondev is offline x10Hosting Member pythondev is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    59

    Re: Ajax Login ajaxForm() issues

    although your post not that informative, it did give me a few idea's...

    I spent a whole lot of time re-writing the code to add both classes and then use $('#mydiv1).show(); plus the hide function and also the jquery.cookie plugin to look for the cookie set or not set to show and hide the different divisions as needed, then put the textresponse into different fields...

    alot of extra bits here and there, but is working the way I intended now... working with legacy code from others is never fun...LOL
    will post the code just in case it is usefull for someone else.

    HTML now looks like this:
    Code:
    <div id="getLog">Welcome User</div>
    <div id="LogErr"></div>
    
    <div id="log_in">
        <form id="loginf" method="post" action="/index.php?lang=en" > <label>Username
    	 <input type="text" name="username" id="username" title="required" size="16" />
             </label>
             <br />
                  <label>Password
              <input type="password" name="password" id="password" title="required" size="16"/>
              </label>
              <input type="hidden"  id="action" name="action" value="login" />
              <input type="hidden" id="ajaxCall" name="ajaxCall" value="1" />
              <input name="login" type="submit" id="login" value="Login"/><br>
    	  Remember Me<input type="checkbox" id="userRemember" name="userRemember" value="yes">
           </form><br><a href="/index.php?p=register">Register New User</a>
    </div>
    
    <div id="log_out">
          <form id="logOut" method="post" action="/index.php?lang=en">
    	<input type="hidden" id="ajaxCall" name="ajaxCall" value="1" />
            <input type="hidden" id="action" name="action" value="logout" />
            <input name="logout" type="submit" id="logout" value="Log Out" /><br>
          </form>
    </div>
    And JQuery Looks like this
    Code:
    $(document).ready(function()
    {
    
        var COOKIE_NAME = 'User-Name';
    
        if ($.cookie( COOKIE_NAME) != undefined){ // check the value // returns test
    	$('#log_in').hide();
    	$('#log_out').show();
    	} else {
    	$('#log_out').hide();
    	$('#log_in').show();
    	}
    
    $('#loginf').ajaxForm({
    			target: '#getLog',
    			resetForm: true,
    			beforeSubmit: function () { $('#log_in').hide(); $('#getLog').html('<img src="/img/ajax-loader.gif">'); },
    			success:       function () { 
    			$('#LogErr').removeClass().addClass('LogErr').html(' ')
    			 if ($.cookie( 'User-Name' ) != undefined){ // check the value // returns test
    			$('#log_in').hide();
    			$('#log_out').show();
    			$('#LogErr').removeClass().addClass('LogErr').html(' ')
    			showResponse; 
    			} else {
    			$('#log_out').hide();
    			$('#log_in').show();
    			$('#LogErr').removeClass().addClass('errorMsg').html('Username or Password Invalid!')
    			showResponse; 
    			}
    			
    			}
    			
    	
    		});
    
    $('#logOut').ajaxForm({
    			target: '#getLog',
    			beforeSubmit: function () { $('#log_out').hide(); $('#getLog').html('<img src="/img/ajax-loader.gif">'); },
    			success:       function () {  $('#log_in').show(); showResponse;}
    		});
    
    });
    jscript includes are:
    Code:
    <script type="text/javascript" src="/templates/jquery.js"></script>
    <script type="text/javascript" src="/templates/jquery.form.js"></script>
    <script type="text/javascript" src="/templates/jquery.cookie.js"></script>
    hope it is usefull to someone...
    Last edited by pythondev; 10-25-2009 at 01:00 PM.

  6. #6
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Ajax Login ajaxForm() issues

    Quote Originally Posted by misson View Post
    When the page loads, #logOut doesn't exist, so the call to $('#logOut').ajaxForm() fails.



    Did you work on the problem with #logOut not existing when the argument to $(document).ready(...) is invoked?
    In jQuery, $(document).ready() is called after the page is loaded, so that is not an issue.
    Last edited by descalzo; 10-25-2009 at 03:29 PM.
    Nothing is always absolutely so.

  7. #7
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Ajax Login ajaxForm() issues

    Quote Originally Posted by pythondev View Post
    although your post not that informative, it did give me a few idea's...
    Where's the fun if I give away all the answers? In truth, your code was advanced enough that I figured you only needed a nudge to work out a solution, and you did. Besides, I was curious to see what you'd come up with.

    Quote Originally Posted by descalzo View Post
    In jQuery, $(document).ready() is called after the page is loaded, so that is not an issue.
    It is an issue if content doesn't exist when the page is loaded, such as when it's the result of an AJAX query. Note that loginDisp() calls exactly one of doLogin() or doLogout(), which generate #loginf and #logOut, respectively. When $_POST['ajaxCall'] is set, the script outputs the result of loginDisp() and (it's a fair bet) nothing else. Note also that the result of each form is placed in #getLog. These facts, along with the behavior pythondev mentions, suggests that the forms are loaded into the page after the document finishes loading, except for one instance that's loaded when the page is.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  8. #8
    pythondev is offline x10Hosting Member pythondev is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    59

    Re: Ajax Login ajaxForm() issues

    actually both of you are right and wrong.. what started me on this with jQuery was browser compatability and the way different browsers handle javascript.. for example the "Original javascript" I wrote works fine in IE and Firefox but not Chrome. so I had to come up with a new way of doing it that was cross compatable with all browsers.

    Original pure javascript code(this doesn't care if logOut not loaded)
    Code:
    // Callback function to handle the event.
      function getForm(fobj) {  
        var str = "";  
        var ft = "";  
        var fv = "";  
        var fn = "";  
        var els = "";  
        for(var i = 0;i < fobj.elements.length;i++) {  
         els = fobj.elements[i];  
         ft = els.title;  
         fv = els.value;  
         fn = els.name;  
        switch(els.type) {  
         case "text":  
         case "hidden":  
         case "password":  
         case "textarea":  
         // is it a required field?  
         if(encodeURI(ft) == "required" && encodeURI(fv).length < 1) {  
           alert('\''+fn+'\' is a required field, please complete.');  
           els.focus();  
           return false;  
         }  
         str += fn + "=" + encodeURI(fv) + "&";  
         break;   
         
         case "checkbox":  
         case "radio":  
          if(els.checked) str += fn + "=" + encodeURI(fv) + "&";  
         break;      
         
         case "select-one":  
           str += fn + "=" +  
           els.options[els.selectedIndex].value + "&";  
         break;  
         } // switch  
        } // for  
        str = str.substr(0,(str.length - 1));  
        return str;  
       }  
       
          var req = createXMLHttpRequest();  
          
        function createXMLHttpRequest() {  
         var ua;  
         if(window.XMLHttpRequest) {  
         try {  
          ua = new XMLHttpRequest();  
         } catch(e) {  
          ua = false;  
        }  
        } else if(window.ActiveXObject) {  
         try {  
           ua = new ActiveXObject("Microsoft.XMLHTTP");  
         } catch(e) {  
           ua = false;  
         }  
        }  
       return ua;  
       }  
    	
    	
    //login Ajax
    	var frm = "";
    	var sFile = "";
    	var file = "";
        function sendRequest(frm, sFile, file) {  
    
        var rnd982g = Math.random();  
        var str = "";  
        var show = document.getElementById(sFile);
    
        if(str = getForm(frm) ) {  
    	
         req.open('POST', file, true);
         req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");  
         req.setRequestHeader("Content-length", str.length);
         req.setRequestHeader("Connection", "close");
          
         req.onreadystatechange = function() {
      if (req.readyState == 4 && req.status == 200) {
       show.innerHTML = req.responseText;
       return false; 
      }else{
       if (req.readyState == 4 && req.status == 404) {
        show.innerHTML = "Page Not Found";
        return false; 
       }else{
        show.innerHTML = "<img src=\"/img/ajax-loader.gif\"><br>Loading ...";
        return false; 
       }
      }
     }
         req.send(str+'&ajaxPost='+rnd982g);
        req.close; 
        }  
        return false;  
       }

  9. #9
    pythondev is offline x10Hosting Member pythondev is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    59

    Re: Ajax Login ajaxForm() issues

    Arkham's Razor: The simplest explanation is the best.
    after doing all that stuff I found that the whole thing didn't work with session variables(server side)
    so I had to rethink again and went back to my original code...

    here are the changes i made to my original code...

    Code:
    $(document).ready(function()
    {
    $('#loginf').ajaxForm({
                beforeSubmit: function () { $('#getLog').html('<img src="/img/ajax-loader.gif">');},
                target: '#getLog',
                success:       function () {  
                var response = showResponse;
                if (response != undefined){
                	$('#getLog').html(showResponse).fadeIn("slow");
    
                	} 
                }
            });
    
    });
    php changes:
    PHP Code:
    $s_doLogin $this->loginDisp();

            if (isset(
    $_POST['ajaxCall']) && $_POST['ajaxCall'] == '1'){
            echo 
    $s_doLogin;
            die;
            }

    public function 
    loginDisp(){
            if (
    $this->obj_protect->showPage){
            
    $curUser=$this->obj_protect->getUser();
            
    $s_doLogin "<span id=\"userLog\">Welcome<br>".$curUser."</span><br><br>\r\n";
            
    $s_register $this->obj_language->get("language/ucPanel");
            
    $s_doLogin .= "<a href=\"/cp/\">".$s_register."</a><br>\r\n";
            
            if (
    $this->obj_protect->checkAdmin() == "1"){
            
    $s_doLogin .= "<a href=\"/adm/\">Admin</a><br>\r\n";
            }
            
    $s_doLogin .= $this->obj_layouts->doLogout();
            
            } else {
            
            
    $s_register $this->obj_language->get("language/register");
            
    $s_user $this->obj_language->get("language/login/username");
            
    $s_pass $this->obj_language->get("language/login/pass");
            
    $s_remMe $this->obj_language->get("language/logRem");

            
    $s_doLogin $this->obj_layouts->doLogin($s_user$s_pass$s_remMe);
            
    $curUser=$this->obj_protect->checkLogin();
            
    $s_errMsg $this->obj_language->get("language/userPass");
            
    $s_curUserstr_replace("{userPass}"$s_errMsg ,$curUser);
            
    $s_doLogin .= $s_curUser;
            
    $s_doLogin .= "<br><a href=\"/index.php?p=register\">$s_register</a>";
            

            
            }
            return 
    $s_doLogin;

    and the forms (this is the important part)
    PHP Code:
    public function doLogin($s_user$s_pass$s_remember="Remember"){
        
    $getURI $_SERVER['REQUEST_URI'];

        
    $s_return "<label>".$s_user."
            <input type=\"text\" name=\"username\" id=\"username\" title=\"required\" size=\"16\" />
          </label>
          <br />
                  <label>"
    .$s_pass."
              <input type=\"password\" name=\"password\" id=\"password\" title=\"required\" size=\"16\"/>
            </label>
            <input type=\"hidden\"  id=\"action\" name=\"action\" value=\"login\" />
            <input type=\"hidden\" id=\"ajaxCall\" name=\"ajaxCall\" value=\"1\" />
            <input name=\"login\" type=\"submit\" id=\"login\" value=\"Login\"/><br>
        "
    .$s_remember."<input type=\"checkbox\" id=\"userRemember\" name=\"userRemember\" value=\"yes\">
           "
    ;
        return 
    $s_return;
        }

        public function 
    doLogout(){
        
    $getURI $_SERVER['REQUEST_URI'];
        
    $s_return "<input type=\"hidden\" id=\"ajaxCall\" name=\"ajaxCall\" value=\"1\" />
            <input type=\"hidden\" id=\"action\" name=\"action\" value=\"logout\" />
            <input name=\"logout\" type=\"submit\" id=\"logout\" value=\"Log Out\" /><br>
          "
    ;
        return 
    $s_return;
        } 
    You will notice I removed the form line!!!
    HTML now looks like this.....
    Code:
      <table width="140" align="left" border="0" cellspacing="0" cellpadding="0" id="sidebar1">
      <tr>
        <td align="center" height="150">
    <form id="loginf" method="post" action="index.php">
    <div id="getLog">{LOGIN}</div></form>
          </td>
      </tr>
      <tr>
        <td align="center" valign="top">{SIDEBAR}</td>
      </tr>
    </table>
    I placed the form tag OUTSIDE of the "getLog" division....

    Now all works perfect and less effort then the original re-write...:facesjump
    Last edited by pythondev; 10-27-2009 at 05:55 AM.

  10. #10
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Ajax Login ajaxForm() issues

    Much nicer and cleaner.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Replies: 1
    Last Post: 08-30-2008, 02:18 AM
  2. FTP Login issues
    By Marc351 in forum Free Hosting
    Replies: 2
    Last Post: 08-22-2008, 03:41 AM
  3. Unable to login properly,
    By icefire in forum Free Hosting
    Replies: 3
    Last Post: 02-06-2008, 10:17 PM
  4. Control Panel Login Issues?
    By ShoutX in forum Free Hosting
    Replies: 2
    Last Post: 09-19-2007, 08:21 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers