www.webdeveloper.com
Results 1 to 3 of 3

Thread: Redirect after login doesn't work

  1. #1
    Join Date
    Jan 2013
    Posts
    84

    Unhappy Redirect after login doesn't work

    Hi
    for what reason this code doesn't work?

    PHP Code:
    <?php

    $f_usr
    $_POST["username"];
    $f_pswd$_POST["password"];

    $con=mysql_connect("localhost","americanhorizon","")
    or die(
    "Unable to connect to MySQL");


    //select a database to work with
    $selected mysql_select_db("my_americanhorizon",$con)
      or die(
    "Could not select my_americanhorizon");

    $sql="SELECT * FROM negozi WHERE Username='$f_usr'";
    $result=mysql_query($sql,$con);


    $datiUtente=mysql_fetch_assoc($result);

    if(
    $datiUtente==array()) // if empty array
    {
      echo(
    "Sorry");
    }
    else
    {
       
    //$_SESSION['datiUtente']=$datiUtente;
       
    header("Location: userPanel.php"); //successfully logged
    }

    mysql_close($con);

    ?>
    in the php page that contains the form, the "Sorry" response is correctly processed, while header("Location: userPanel.php"); for redirect the page to the userpanel if successfully logged doesn't work


    This is the form page (it's not necessary that you read it, it's should be correct. Only one doubt, i wrote the condition "if (ajaxReq.responseText == "Sorry")"... Maybe i've to write a condition in case of redirection too?)

    HTML Code:
     <div id="logindAndRegistra">
         <div id="sezionesx">
           <div class="scritta" id="loginHeader">Login</div><br />
           <div class="scritta">eMail <br /><input id="username"/></div><br />
           <div class="scritta">Password <br /><input id="password"/></div><br />
           <button id="loginVai">Vai -></button>
           
         </div>
    
    <script>
    $('#loginVai').click( function() 
    					 {
    						 var ajaxReq = new XMLHttpRequest();
    						 
    						 var params = "username="+$('#username').val()+"&password="+$('#password').val();
    						 var url = "tentaLogin.php";
    						 ajaxReq.open("POST", url, true);
    						 
    						 ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    						 ajaxReq.setRequestHeader("Content-length", params.length);
    						 ajaxReq.setRequestHeader("Connection", "close");
    
    						 ajaxReq.onreadystatechange = function() 
    													  {
    														
    														  if(ajaxReq.readyState == 4 && ajaxReq.status == 200) 
    														  {
    															  alert(ajaxReq.responseText)
    															  if (ajaxReq.responseText == "Sorry")
    															  {
    																  $("#loginSbagliato").remove(); 
    																  $('#sezionesx').append($('<div id="loginSbagliato" class="baloon up scritta">I dati inseriti sono errati</div>'))
    															      $('#loginSbagliato').hide();
    																  $('#loginSbagliato').show(400);
    															  }
    															  
    																												  
    														  }
    													   }
    						  
    						  ajaxReq.send(params);
    				   
    					 }
    				   
    				   )
    Last edited by American horizo; 04-14-2013 at 08:30 AM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,222
    I don't think you can process a redirect header through AJAX (the response would actually be the page you redirect to). Instead I might send a JSON object as the response from the PHP script, and then the AJAX response handler would decide what to do based on what is in that response, including a window.location call to the specified URL if that's what the response specifies.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Apr 2013
    Posts
    56
    Like NogDog said. Echo out url and use it in your ajax processing function. Put an else there. If it is not "sorry" then you redirect to logged in page.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles