Login with ajax jquery












0















I have a problem with a simple login with ajax, apparently all is right, but I don´t know what is wrong, because I need to send a message to the client indicating that the user info is right and then redirect to main page.



Source Code



<!DOCTYPE html>
<html lang="es" >
<head>
<meta http-equiv='Content-type' content='text/html; charset=<? echo APP_CHARSET ?>' />
<title>Red Social Basica</title>
<? Tag::css('reset') ?>
<? Tag::css('design') ?>
<? echo Html::includeCss() ?>
<? echo Tag::js('jquery/jquery.min') ?>
</head>
<body class="fondobody">

<header class="topbar">
<a href="" class="bntesatico" id="lnklogin">Login</a>
<div id="dvlogeo">
User/Email<input type="text" id="txtusremail"/>
Password<input type="password" id="txtpassword"/>
<a href="" id="lnkenter">Enter</a>
</div>
<a href="" class="bntesatico" id="lnkregistro">Sign up</a>

</header>
</body>
</html>
<script type="text/javascript">

$(document).ready(function(){
$('#lnklogin').click(function() {
$('#dvlogeo').fadeToggle(200, function() {
var divID = $('#dvlistipopubs');
var openDiv = $(this).is(':visible') ? divID : null;
});
return false;
});

$('#lnkenter').click(function() {
var usremail=$('#txtusremail').val(),
password=$('#txtpassword').val();
$.ajax({
type: 'POST',
url: "account/login.php",
data: 'usremail=' + usremail +
'&password=' + password,
success: function(data) {
if(data=='ok'){
document.location="account/main.php"
}else{
alert('Access denied');
}
}
});
return false;
});
});
</script>


I used firebug to verify the answer from the server and is printing 'ok' if user and password are right and error if something is wrong, the problem is that is not working here



success: function(data) {
if(data=='ok'){
document.location="account/main.php"
}else{
alert('Access denied');
}
}









share|improve this question

























  • can you show your php file?

    – Drixson Oseña
    Nov 28 '13 at 15:11






  • 2





    what does console.log(data) returns?, before if(data==....

    – Cyrus
    Nov 28 '13 at 15:12











  • Does success even trigger? If the response is not a 200 OK, only complete will trigger, not success. Firebug has a Network tab. Check that for the response.

    – Rudie
    Nov 28 '13 at 15:31











  • Here is my php file, is a method because I am using MVC -> kupaste.com/ver/64167

    – Alexander Ceballos
    Nov 28 '13 at 15:32













  • The response is 200 OK

    – Alexander Ceballos
    Nov 28 '13 at 15:34
















0















I have a problem with a simple login with ajax, apparently all is right, but I don´t know what is wrong, because I need to send a message to the client indicating that the user info is right and then redirect to main page.



Source Code



<!DOCTYPE html>
<html lang="es" >
<head>
<meta http-equiv='Content-type' content='text/html; charset=<? echo APP_CHARSET ?>' />
<title>Red Social Basica</title>
<? Tag::css('reset') ?>
<? Tag::css('design') ?>
<? echo Html::includeCss() ?>
<? echo Tag::js('jquery/jquery.min') ?>
</head>
<body class="fondobody">

<header class="topbar">
<a href="" class="bntesatico" id="lnklogin">Login</a>
<div id="dvlogeo">
User/Email<input type="text" id="txtusremail"/>
Password<input type="password" id="txtpassword"/>
<a href="" id="lnkenter">Enter</a>
</div>
<a href="" class="bntesatico" id="lnkregistro">Sign up</a>

</header>
</body>
</html>
<script type="text/javascript">

$(document).ready(function(){
$('#lnklogin').click(function() {
$('#dvlogeo').fadeToggle(200, function() {
var divID = $('#dvlistipopubs');
var openDiv = $(this).is(':visible') ? divID : null;
});
return false;
});

$('#lnkenter').click(function() {
var usremail=$('#txtusremail').val(),
password=$('#txtpassword').val();
$.ajax({
type: 'POST',
url: "account/login.php",
data: 'usremail=' + usremail +
'&password=' + password,
success: function(data) {
if(data=='ok'){
document.location="account/main.php"
}else{
alert('Access denied');
}
}
});
return false;
});
});
</script>


I used firebug to verify the answer from the server and is printing 'ok' if user and password are right and error if something is wrong, the problem is that is not working here



success: function(data) {
if(data=='ok'){
document.location="account/main.php"
}else{
alert('Access denied');
}
}









share|improve this question

























  • can you show your php file?

    – Drixson Oseña
    Nov 28 '13 at 15:11






  • 2





    what does console.log(data) returns?, before if(data==....

    – Cyrus
    Nov 28 '13 at 15:12











  • Does success even trigger? If the response is not a 200 OK, only complete will trigger, not success. Firebug has a Network tab. Check that for the response.

    – Rudie
    Nov 28 '13 at 15:31











  • Here is my php file, is a method because I am using MVC -> kupaste.com/ver/64167

    – Alexander Ceballos
    Nov 28 '13 at 15:32













  • The response is 200 OK

    – Alexander Ceballos
    Nov 28 '13 at 15:34














0












0








0








I have a problem with a simple login with ajax, apparently all is right, but I don´t know what is wrong, because I need to send a message to the client indicating that the user info is right and then redirect to main page.



Source Code



<!DOCTYPE html>
<html lang="es" >
<head>
<meta http-equiv='Content-type' content='text/html; charset=<? echo APP_CHARSET ?>' />
<title>Red Social Basica</title>
<? Tag::css('reset') ?>
<? Tag::css('design') ?>
<? echo Html::includeCss() ?>
<? echo Tag::js('jquery/jquery.min') ?>
</head>
<body class="fondobody">

<header class="topbar">
<a href="" class="bntesatico" id="lnklogin">Login</a>
<div id="dvlogeo">
User/Email<input type="text" id="txtusremail"/>
Password<input type="password" id="txtpassword"/>
<a href="" id="lnkenter">Enter</a>
</div>
<a href="" class="bntesatico" id="lnkregistro">Sign up</a>

</header>
</body>
</html>
<script type="text/javascript">

$(document).ready(function(){
$('#lnklogin').click(function() {
$('#dvlogeo').fadeToggle(200, function() {
var divID = $('#dvlistipopubs');
var openDiv = $(this).is(':visible') ? divID : null;
});
return false;
});

$('#lnkenter').click(function() {
var usremail=$('#txtusremail').val(),
password=$('#txtpassword').val();
$.ajax({
type: 'POST',
url: "account/login.php",
data: 'usremail=' + usremail +
'&password=' + password,
success: function(data) {
if(data=='ok'){
document.location="account/main.php"
}else{
alert('Access denied');
}
}
});
return false;
});
});
</script>


I used firebug to verify the answer from the server and is printing 'ok' if user and password are right and error if something is wrong, the problem is that is not working here



success: function(data) {
if(data=='ok'){
document.location="account/main.php"
}else{
alert('Access denied');
}
}









share|improve this question
















I have a problem with a simple login with ajax, apparently all is right, but I don´t know what is wrong, because I need to send a message to the client indicating that the user info is right and then redirect to main page.



Source Code



<!DOCTYPE html>
<html lang="es" >
<head>
<meta http-equiv='Content-type' content='text/html; charset=<? echo APP_CHARSET ?>' />
<title>Red Social Basica</title>
<? Tag::css('reset') ?>
<? Tag::css('design') ?>
<? echo Html::includeCss() ?>
<? echo Tag::js('jquery/jquery.min') ?>
</head>
<body class="fondobody">

<header class="topbar">
<a href="" class="bntesatico" id="lnklogin">Login</a>
<div id="dvlogeo">
User/Email<input type="text" id="txtusremail"/>
Password<input type="password" id="txtpassword"/>
<a href="" id="lnkenter">Enter</a>
</div>
<a href="" class="bntesatico" id="lnkregistro">Sign up</a>

</header>
</body>
</html>
<script type="text/javascript">

$(document).ready(function(){
$('#lnklogin').click(function() {
$('#dvlogeo').fadeToggle(200, function() {
var divID = $('#dvlistipopubs');
var openDiv = $(this).is(':visible') ? divID : null;
});
return false;
});

$('#lnkenter').click(function() {
var usremail=$('#txtusremail').val(),
password=$('#txtpassword').val();
$.ajax({
type: 'POST',
url: "account/login.php",
data: 'usremail=' + usremail +
'&password=' + password,
success: function(data) {
if(data=='ok'){
document.location="account/main.php"
}else{
alert('Access denied');
}
}
});
return false;
});
});
</script>


I used firebug to verify the answer from the server and is printing 'ok' if user and password are right and error if something is wrong, the problem is that is not working here



success: function(data) {
if(data=='ok'){
document.location="account/main.php"
}else{
alert('Access denied');
}
}






jquery ajax login






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '13 at 15:22









Cyrus

1047




1047










asked Nov 28 '13 at 15:02









Alexander CeballosAlexander Ceballos

64811328




64811328













  • can you show your php file?

    – Drixson Oseña
    Nov 28 '13 at 15:11






  • 2





    what does console.log(data) returns?, before if(data==....

    – Cyrus
    Nov 28 '13 at 15:12











  • Does success even trigger? If the response is not a 200 OK, only complete will trigger, not success. Firebug has a Network tab. Check that for the response.

    – Rudie
    Nov 28 '13 at 15:31











  • Here is my php file, is a method because I am using MVC -> kupaste.com/ver/64167

    – Alexander Ceballos
    Nov 28 '13 at 15:32













  • The response is 200 OK

    – Alexander Ceballos
    Nov 28 '13 at 15:34



















  • can you show your php file?

    – Drixson Oseña
    Nov 28 '13 at 15:11






  • 2





    what does console.log(data) returns?, before if(data==....

    – Cyrus
    Nov 28 '13 at 15:12











  • Does success even trigger? If the response is not a 200 OK, only complete will trigger, not success. Firebug has a Network tab. Check that for the response.

    – Rudie
    Nov 28 '13 at 15:31











  • Here is my php file, is a method because I am using MVC -> kupaste.com/ver/64167

    – Alexander Ceballos
    Nov 28 '13 at 15:32













  • The response is 200 OK

    – Alexander Ceballos
    Nov 28 '13 at 15:34

















can you show your php file?

– Drixson Oseña
Nov 28 '13 at 15:11





can you show your php file?

– Drixson Oseña
Nov 28 '13 at 15:11




2




2





what does console.log(data) returns?, before if(data==....

– Cyrus
Nov 28 '13 at 15:12





what does console.log(data) returns?, before if(data==....

– Cyrus
Nov 28 '13 at 15:12













Does success even trigger? If the response is not a 200 OK, only complete will trigger, not success. Firebug has a Network tab. Check that for the response.

– Rudie
Nov 28 '13 at 15:31





Does success even trigger? If the response is not a 200 OK, only complete will trigger, not success. Firebug has a Network tab. Check that for the response.

– Rudie
Nov 28 '13 at 15:31













Here is my php file, is a method because I am using MVC -> kupaste.com/ver/64167

– Alexander Ceballos
Nov 28 '13 at 15:32







Here is my php file, is a method because I am using MVC -> kupaste.com/ver/64167

– Alexander Ceballos
Nov 28 '13 at 15:32















The response is 200 OK

– Alexander Ceballos
Nov 28 '13 at 15:34





The response is 200 OK

– Alexander Ceballos
Nov 28 '13 at 15:34












2 Answers
2






active

oldest

votes


















0














Try using this code with function (responseText) ;)



success: function (responseText) { // Get the result and assign to each case.
if (responseText == 'ok') {
document.location = "account/main.php";
} else {
alert('Access denied');
}
}
});





share|improve this answer

































    0














    Here's the solution I found, using JSON.



    In my Ajax:



     $.ajax({type: 'GET',
    dataType: 'json',
    url: "login.php",
    data: 'usremail=' + usremail +
    '&clave=' + clave
    }).done(function(data) {
    if (data.mensaje == "ok") {
    document.location = "index.php";
    } else {
    $('#dvmsjlogin').html(data.mensaje);
    }
    });


    And in my PHP:



    if ($auth->authenticate()) {
    $res['mensaje'] = 'ok';
    }else{
    $res['mensaje']='error';
    }

    echo json_encode($res);





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20269281%2flogin-with-ajax-jquery%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Try using this code with function (responseText) ;)



      success: function (responseText) { // Get the result and assign to each case.
      if (responseText == 'ok') {
      document.location = "account/main.php";
      } else {
      alert('Access denied');
      }
      }
      });





      share|improve this answer






























        0














        Try using this code with function (responseText) ;)



        success: function (responseText) { // Get the result and assign to each case.
        if (responseText == 'ok') {
        document.location = "account/main.php";
        } else {
        alert('Access denied');
        }
        }
        });





        share|improve this answer




























          0












          0








          0







          Try using this code with function (responseText) ;)



          success: function (responseText) { // Get the result and assign to each case.
          if (responseText == 'ok') {
          document.location = "account/main.php";
          } else {
          alert('Access denied');
          }
          }
          });





          share|improve this answer















          Try using this code with function (responseText) ;)



          success: function (responseText) { // Get the result and assign to each case.
          if (responseText == 'ok') {
          document.location = "account/main.php";
          } else {
          alert('Access denied');
          }
          }
          });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 24 '18 at 2:14









          Davіd

          3,67041735




          3,67041735










          answered May 12 '14 at 5:03









          user3627103user3627103

          1




          1

























              0














              Here's the solution I found, using JSON.



              In my Ajax:



               $.ajax({type: 'GET',
              dataType: 'json',
              url: "login.php",
              data: 'usremail=' + usremail +
              '&clave=' + clave
              }).done(function(data) {
              if (data.mensaje == "ok") {
              document.location = "index.php";
              } else {
              $('#dvmsjlogin').html(data.mensaje);
              }
              });


              And in my PHP:



              if ($auth->authenticate()) {
              $res['mensaje'] = 'ok';
              }else{
              $res['mensaje']='error';
              }

              echo json_encode($res);





              share|improve this answer






























                0














                Here's the solution I found, using JSON.



                In my Ajax:



                 $.ajax({type: 'GET',
                dataType: 'json',
                url: "login.php",
                data: 'usremail=' + usremail +
                '&clave=' + clave
                }).done(function(data) {
                if (data.mensaje == "ok") {
                document.location = "index.php";
                } else {
                $('#dvmsjlogin').html(data.mensaje);
                }
                });


                And in my PHP:



                if ($auth->authenticate()) {
                $res['mensaje'] = 'ok';
                }else{
                $res['mensaje']='error';
                }

                echo json_encode($res);





                share|improve this answer




























                  0












                  0








                  0







                  Here's the solution I found, using JSON.



                  In my Ajax:



                   $.ajax({type: 'GET',
                  dataType: 'json',
                  url: "login.php",
                  data: 'usremail=' + usremail +
                  '&clave=' + clave
                  }).done(function(data) {
                  if (data.mensaje == "ok") {
                  document.location = "index.php";
                  } else {
                  $('#dvmsjlogin').html(data.mensaje);
                  }
                  });


                  And in my PHP:



                  if ($auth->authenticate()) {
                  $res['mensaje'] = 'ok';
                  }else{
                  $res['mensaje']='error';
                  }

                  echo json_encode($res);





                  share|improve this answer















                  Here's the solution I found, using JSON.



                  In my Ajax:



                   $.ajax({type: 'GET',
                  dataType: 'json',
                  url: "login.php",
                  data: 'usremail=' + usremail +
                  '&clave=' + clave
                  }).done(function(data) {
                  if (data.mensaje == "ok") {
                  document.location = "index.php";
                  } else {
                  $('#dvmsjlogin').html(data.mensaje);
                  }
                  });


                  And in my PHP:



                  if ($auth->authenticate()) {
                  $res['mensaje'] = 'ok';
                  }else{
                  $res['mensaje']='error';
                  }

                  echo json_encode($res);






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 24 '18 at 2:15









                  Davіd

                  3,67041735




                  3,67041735










                  answered Dec 11 '13 at 23:07









                  Alexander CeballosAlexander Ceballos

                  64811328




                  64811328






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20269281%2flogin-with-ajax-jquery%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Feedback on college project

                      Futebolista

                      Albești (Vaslui)