0

I have login and signup form and i made HTML form to work by switching between between login and signup. I successfully tried console of signup input data but I am facing problem in Login Form. I am not able to see any input data in console.

CLick here to see my login interface

I have file register.php with below code.

<body>
        <div class="container" style='margin-top:100px;'>
            <div class="row">
                <div class="col-sm-12">
                    <div class="form-wrap">
                        <div class="tabs">
                            <h3 class="signup-tab"><a class="active" href="#signup-tab-content">Sign Up</a></h3>
                            <h3 class="login-tab"><a href="#login-tab-content">Login</a></h3>
                        </div><!--a.tabs-->

                        <div class="tabs-content">
                            <div id="signup-tab-content" class="active">
                                <form class="signup-form" action="" method="post">
                                    <input type="email" class="input" name="user_email" autocomplete="off" placeholder="Email">
                                    <input type="text" class="input" name="user_name" autocomplete="off" placeholder="Username">
                                    <input type="password" class="input" name="user_pass" autocomplete="off" placeholder="Password">
                                    <input type="submit" class="button" name="registersubmit" id="registersubmit" value="Sign Up">
                                </form><!--.login-form-->
                                <div class="message_box" style="margin:10px 0px;"></div>
                                <div class="help-text">
                                    <p>By signing up, you agree to our</p>
                                    <p><a href="#">Terms of service</a></p>
                                </div><!--.help-text-->
                            </div><!--.signup-tab-content-->

                            <div id="login-tab-content">
                                <form class="login-form" action="" method="post">
                                    <input type="email" class="input" name="user_email" autocomplete="off" placeholder="Email">
                                    <input type="password" class="input" name="user_pass" autocomplete="off" placeholder="Password">
                                    <input type="checkbox" class="checkbox" id="remember_me">
                                    <label for="remember_me">Remember me</label>

                                    <input type="submit" class="button" id="loginsubmit" name="loginsubmit" value="Login">
                                </form><!--.login-form-->
                                <div class="message_box" style="margin:10px 0px;"></div>
                                <div class="help-text">
                                    <p><a href="#">Forget your password?</a></p>
                                </div><!--.help-text-->
                            </div><!--.login-tab-content-->
                        </div><!--.tabs-content-->
                    </div><!--.form-wrap-->


                </div>

            </div>
        </div>

ended with script as follows:

<script>
        jQuery(document).ready(function($) {
            tab = $('.tabs h3 a');

            tab.on('click', function(event) {
                event.preventDefault();
                tab.removeClass('active');
                $(this).addClass('active');

                tab_content = $(this).attr('href');
                $('div[id$="tab-content"]').removeClass('active');
                $(tab_content).addClass('active');
            });
        });


        </script>

        <script>
            $(document).ready(function() {

               var delay = 2000;

               $('[name="registersubmit"]').click(function(e){

                   e.preventDefault();

                   var user_name = $('[name="user_name"]').val();

                   console.log(user_name);

                   if(user_name == ''){
                       $('.message_box').html(
                       '<span style="color:red;">Enter Your Name!</span>'
                       );
                       $('[name="user_name"]').focus();
                       return false;
                   }

                   var user_email = $('[name="user_email"]').val();

                   console.log(user_email);

                   if(user_email == ''){
                       $('.message_box').html(
                       '<span style="color:red;">Enter Email Address!</span>'
                       );
                       $('[name="user_email"]').focus();
                       return false;
                   }

                   if( $('[name="user_email"]').val()!='' ){
                       if( !isValidEmailAddress( $('[name="user_email"]').val() ) ){
                           $('.message_box').html(
                           '<span style="color:red;">Provided email address is incorrect!</span>'
                           );
                           $('[name="user_email"]').focus();
                           return false;
                       }
                   }

                   var user_pass = $('[name="user_pass"]').val();

                   if(user_pass == ''){
                       $('.message_box').html(
                       '<span style="color:red;">Password cannot be empty!</span>'
                       );
                       $('[name="user_pass"]').focus();
                          return false;
                   } 

                   console.log(user_pass);

                   $.ajax
                           ({
                           type: "POST",
                           url: "registersubmit.php",
                           data: "name="+user_name+"&email="+user_email+"&password="+user_pass,
                           beforeSend: function() {
                           $('.message_box').html(
                           '<img src="tenor.gif" width="25" height="25"/>'
                           );
                           }, 
                           success: function(data)
                           {
                           setTimeout(function() {
                           $('.message_box').html(data);
                           }, delay);
                           }
                           });



            });

            $('[name="loginsubmit"]').click(function(e){

                        e.preventDefault();

                    //  var user_name = $('[name="user_name"]').val();

                    //   console.log(user_name);

                    //   if(user_name == ''){
                    //       $('.message_box').html(
                    //       '<span style="color:red;">Enter Your Name!</span>'
                    //       );
                    //       $('[name="user_name"]').focus();
                    //       return false;
                    //   }

                       var user_email = $('[name="user_email"]').val();

                       console.log(user_email);

                       if(user_email == ''){
                           $('.message_box').html(
                           '<span style="color:red;">Enter Email Address!</span>'
                           );
                           $('[name="user_email"]').focus();
                           return false;
                       }

                       if( $('[name="user_email"]').val()!='' ){
                           if( !isValidEmailAddress( $('[name="user_email"]').val() ) ){
                               $('.message_box').html(
                               '<span style="color:red;">Provided email address is incorrect!</span>'
                               );
                               $('[name="user_email"]').focus();
                               return false;
                           }
                       }

                       var user_pass = $('[name="user_pass"]').val();

                       if(user_pass == ''){
                           $('.message_box').html(
                           '<span style="color:red;">Password cannot be empty!</span>'
                           );
                           $('[name="user_pass"]').focus();
                              return false;
                       } 

                       console.log(user_pass);

                        $.ajax({
                                   type: "POST",
                                   url: "loginsubmit.php",
                                //   data: "name="+user_name+"&email="+user_email+"&password="+user_pass,
                                   data: "email="+user_email+"&password="+user_pass,
                                   beforeSend: function() {
                                   $('.message_box').html(
                                   '<img src="tenor.gif" width="25" height="25"/>'
                                   );
                                   }, 
                                   success: function(data)
                                   {
                                   setTimeout(function() {
                                   $('.message_box').html(data);
                                   }, delay);
                                   }
                                   });


                    });

            });


            </script>
            <script>
                //Email Validation Function 
                function isValidEmailAddress(emailAddress) {
                    var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
                    return pattern.test(emailAddress);
                };
            </script>

    </body>

Also i have the code where i will get my login input data and match password and display user dashboard on successful login. Code of loginsubmit.php is as below.

<?php


// if ( ($_POST['name']!="") && ($_POST['email']!="")){
if ( ($_POST['email']!="")){
// $name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];

include('connection.php');

//Password Match

$passwordhash = password_hash($password, PASSWORD_BCRYPT);

$query = "SELECT * FROM `tbl_register` WHERE email = '$email' AND  password = '$passwordhash'";



//PHP code to redirect to dashboard if login successfull else redirect login page



}



?>
  • 1
    **Warning!** You are _wide open_ for SQL injection attacks! You should use parameterized [prepared statements](https://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead of using completely unescaped user data directly in your queries like that. – M. Eriksson Oct 17 '19 at 06:01
  • That's wrong in query i corrected it but what about console output of login? I am not able to pass it to my receiving php page and also not able to console it. – My Creations Oct 17 '19 at 06:05
  • If you only do `$([name="user_email"])` while having multiple forms on the page that has an input with that name, how would you know which you get? You should either fetch the value from the correct form: `$('#theFormId').find('[name="user_email"]')`, or give all inputs unique id's and fetch using id's instead. – M. Eriksson Oct 17 '19 at 06:15
  • yes that seems correct i need to figure out this first – My Creations Oct 17 '19 at 06:18
  • @MagnusEriksson its Amazing you clicked my problem Thanks it was solved – My Creations Oct 17 '19 at 06:26
  • can you also explain little about the SQL injection warning, can it be prevented by using session while login. – My Creations Oct 17 '19 at 06:28
  • SQL injections are about how you use user data in your SQL queries. It doesn't have anything to do with sessions. You can read more about [SQL injections here](https://www.owasp.org/index.php/SQL_Injection) – M. Eriksson Oct 17 '19 at 06:35
  • This `password_hash()` create different hash each time then how to use it for creating hash of password entered and then match with earlier hash made by the same funciton, becoz each time that function create diff hash. – My Creations Oct 17 '19 at 07:24
  • Yes, it does, but that's by design. To verify a password, you fetch the user with the correct emal from the database. Then, if you get a record, you match the password hash your stored in the database for that user with the password the user just posted from the login form using [password_verify()](https://www.php.net/manual/en/function.password-verify.php). You can check https://stackoverflow.com/a/42614713/2453432 for an example. – M. Eriksson Oct 17 '19 at 12:20
  • @MagnusEriksson thanks you suggested very correctly and it worked boom. – My Creations Oct 18 '19 at 05:08

0 Answers0