dcsimg
www.webdeveloper.com
Results 1 to 15 of 15

Thread: PHP click login

  1. #1
    Join Date
    Aug 2016
    Posts
    7

    Question PHP click login

    I need call PHP function in another file when i click button in modal.
    But that is not work....

    i try this:

    HTML Code:
      <!-- Login Popup-->
    <div class="modal fade" id="myModal" role="dialog">
        <div class="modal-dialog">
        
          <!-- Modal content-->
          <div class="modal-content">
            <div class="modal-header" style="padding:35px 50px;">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4><span class="glyphicon glyphicon-lock"></span> Login</h4>
            </div>
            <div class="modal-body" style="padding:40px 50px;">
            <!---------------------------------------------------------------------------> 
            <!-- Form -->  
            <form role="form" action="Site.php" method="post">
                <div class="form-group">
                  <label for="email"><span class="glyphicon glyphicon-user"></span> Email</label>
                  <input type="email" name="email" class="form-control" id="email" placeholder="Enter email">
                </div>
                <div class="form-group">
                  <label for="psw"><span class="glyphicon glyphicon-eye-open"></span> Password</label>
                  <input type="password" name="password" class="form-control" id="psw" placeholder="Enter password">
                </div>
                <div class="checkbox">
                  <label><input type="checkbox" value="" checked>Remember me</label>
                </div>
                  <button type="button" class="btn btn-success btn-block" onclick="SendLogin()">Login</button> <!--<span class="glyphicon glyphicon-off"></span>-->
              </form>
              <!-- End of Form-->
            </div>
            <!---------------------------------------------------------------------------> 
            <div class="modal-footer">
              <button type="submit" class="btn btn-danger btn-default pull-left" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Cancel</button>
              <p>Not a member? <a href="Register/register.php">Sign Up</a></p>
              <p>Forgot <a href="#">Password?</a></p>
            
            <?php include ('Login/login.php'); ?>
            
            </div>
          </div>
          
        </div>
      </div>
    Code:
    <script>
    function SendLogin(){
        $.ajax({
            url:"Login/login.php",
            type:"POST",
            data: on,
            cache: false,
            success: function(result){
                Login();   
            }
        });
    }
    </script>
    Code:
    <?php 
        
        #If click Login
        function Login ()
        {
            #Empty checks
            if(empty($_POST['email']))
            {
                echo '<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your email cant be empty!" disabled>';
                return false;
            }
            if(empty($_POST['password']))
            {
                echo '<p class="bg-danger">Field with your password cant be empty</p>';
                return false;
            }
        }
        //
    
    ?>

  2. #2
    Join Date
    Aug 2016
    Location
    Georgia
    Posts
    12
    Since you already have a destination for the form items to go to in your form action attribute, why don't you just replace your <button...> with
    <input type="submit" name="submit"> ?

    Since your Login function is going to be on the same page anyways...you can approach this many different ways.

    Here's one:

    if (isset($_POST["submit"])) {
    Login();
    } else {
    ?>
    ... your form ...
    }

    function Login() {
    ...
    }

  3. #3
    Join Date
    Aug 2016
    Posts
    7
    Quote Originally Posted by dinocajic View Post
    Since you already have a destination for the form items to go to in your form action attribute, why don't you just replace your <button...> with
    <input type="submit" name="submit"> ?

    Since your Login function is going to be on the same page anyways...you can approach this many different ways.

    Here's one:

    if (isset($_POST["submit"])) {
    Login();
    } else {
    ?>
    ... your form ...
    }

    function Login() {
    ...
    }
    ok ty.
    <button type="submit" name="submit">Login</button>
    If i use that ^ when I click the button , the page refreshes

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,566
    I believe you want an input tag, not a button tag
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  5. #5
    Join Date
    Aug 2016
    Posts
    7
    ok I tried the button tag and changed some things but now i have 2 problems

    Code:http://textuploader.com/58xdf

    1.If I click a button to refresh the page ( if you use the input tag , then the php not start)
    2.I have a database in phpmyadmin users and I have a user with email and password. But when I enter this information to log in , then tells me that the data is not accurate.

  6. #6
    Join Date
    Aug 2016
    Posts
    7
    Bump

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,566
    As I said before - did you change from <button> to <input>?

    Bump
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  8. #8
    Join Date
    Aug 2016
    Posts
    7
    Yes i change button to input.but still refreshes the page and login does not work as before?

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,566
    So - now you debug. Add some echo statements at key points in your code to tell you where it is going and what it is doing.

    PS - do you have error checking turned on?
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  10. #10
    Join Date
    Aug 2016
    Location
    Stockholm
    Posts
    2
    Instead of printing errors with PHP function Login() if inputs are empty, let jQuery do the job for you (that way it actually works, too!)

    This is how I would have done it:
    Code:
    <script>
    $(document).ready(function() {
    	$("#clicksubmit").click(function() { // when you click login
    		event.preventDefault(); // prevent browser from refreshing after clicking login
    		
    		$.ajax({
    			url:"Login/login.php",
    			type:"POST",
    			data: $(this).serialize(),
    			cache: false,
    			success: function(result){
    				if(!$("#email").val()) {
    					$("#email").replaceWith('<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your email cant be empty!" disabled>');
    				}
    				if(!$("#psw").val()) {
    					$("#psw").replaceWith('<p class="bg-danger">Field with your password cant be empty</p>');
    				}
    			}
    		});
    	});
    });
    </script>
    The form:
    HTML Code:
    <form role="form" action="test.php" method="post" id="the-form">
                <div class="form-group">
                  <label for="email"><span class="glyphicon glyphicon-user"></span> Email</label>
                  <input type="email" name="email" class="form-control" id="email" placeholder="Enter email">
                </div>
                <div class="form-group">
                  <label for="psw"><span class="glyphicon glyphicon-eye-open"></span> Password</label>
                  <input type="password" name="password" class="form-control" id="psw" placeholder="Enter password">
                </div>
                <div class="checkbox">
                  <label><input type="checkbox" value="" checked>Remember me</label>
                </div>
    				<input type="submit" id="clicksubmit" name="sumitbtn" value="Login">
              </form>
    Something to keep in mind though, however: This way you can't use PHP if(isset($_POST["submitbtn"])){...} because you're preventing the browser from reloading the page, which means it can't actually notice if you have sent the form or not. You can still access the $_POST variables in Login/login.php though, without the isset.

    Hope it helps!

  11. #11
    Join Date
    Aug 2016
    Posts
    7
    @Biome

    thank you very much. You have helped me a lot ... but now when i click login button when php function starts then I get the following:
    Undefined index: email
    Undefined index: password


    Code:
     $email = stripcslashes($_POST['email']);
      $password = stripcslashes($_POST['password']);
      $email = mysqli_real_escape_string($dbc, $_POST['email']);
      $password = mysqli_real_escape_string($dbc, $_POST['password']);
    Last edited by jedaisoul; 08-28-2016 at 10:18 AM. Reason: lengthy quote deleted

  12. #12
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,566
    Do you know anything about php or how inputs are passed to php?

    The message is telling you that the array does not have an element by either of those names. So - where are they? Since the array is $_POST it means that your input form does not have those elements (inputs) which means you either did not name them or the name does not match what you are trying to retrieve.

    Does that make sense? If not you need to do some catching up.
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

  13. #13
    Join Date
    Mar 2012
    Posts
    3,635
    Quote Originally Posted by Kosta View Post
    Code:
     $email = stripcslashes($_POST['email']);
      $password = stripcslashes($_POST['password']);
      $email = mysqli_real_escape_string($dbc, $_POST['email']);
      $password = mysqli_real_escape_string($dbc, $_POST['password']);
    I note that you are reading $_POST['password'] twice.

  14. #14
    Join Date
    Aug 2016
    Posts
    7
    I tried to look or php function not to start at all
    Php
    Code:
    <?php 
        include ('Register/config.php'); 
        alert("Test 1"); 
        $email = $_POST['email'];
        $password = $_POST['password'];
    
        alert("Test 2"); 
                  
        //Conver variables
        $email = mysqli_real_escape_string($dbc, $email);
        $password = mysqli_real_escape_string($dbc, $password);
                        
        //Connect to server and select database
        mysqli_select_db($dbc, $db);
                        
        //Qury & database for users
                 
        $res = mysqli_query($dbc, "select * from users where email = '$email' and password = '$password'")
            or die("Filed to qury database".mysqli_error());
                        
        $row = mysqli_fetch_array($res);
        if($row['email'] == $email && $row['password'] == $password)
        {
            alert("Email or password is not currect");
        }
        else 
        {
            alert("Login Complete");   
        }
    
    ?>
    Code:
        if(!email) 
        {
            $("#result1").replaceWith('<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your email cant be empty!" disabled>');
            if(!password) 
            {
            $("#result2").replaceWith('<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your password cant be empty!" disabled>');
            }
            return false;
        }
        if(!password) 
        {
            $("#result2").replaceWith('<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your password cant be empty!" disabled>');
            if(!email) 
            {
              $("#result1").replaceWith('<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your email cant be empty!" disabled>');
            }
            return false;
        }
    
        $.ajax({
          url:"login.php",
          type:"POST",
          data: email, password,
          success: function(){
          }
        });
      });
    });
    </script>
    [code] if(!email)
    {
    $("#result1").replaceWith('<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your email cant be empty!" disabled>');
    if(!password)
    {
    $("#result2").replaceWith('<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your password cant be empty!" disabled>');
    }
    return false;
    }
    if(!password)
    {
    $("#result2").replaceWith('<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your password cant be empty!" disabled>');
    if(!email)
    {
    $("#result1").replaceWith('<input style="text-align:center;"class="form-control" id="disabledInput" type="text" placeholder="Field with your email cant be empty!" disabled>');
    }
    return false;
    }

    $.ajax({
    url:"login.php",
    type:"POST",
    data: email, password,
    success: function(){
    }
    });
    });
    });
    </script>
    Last edited by jedaisoul; 08-28-2016 at 12:15 PM.

  15. #15
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    1,566
    Quote Originally Posted by Kosta View Post
    I tried to look or php function not to start at all
    Php
    I realize that your English is not that good, but this statement makes absolutely no sense. AND since you didn't acknowledge anything that I said in my last post I assume that you are going to work this out on your own.

    Good luck. Good-bye!
    JG
    PS - If you're posting here you should be using:

    error_reporting(E_ALL);
    ini_set('display_errors', '1');


    at the top of ALL php code while you develop it!

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