matthijs110 Posted April 26, 2014 Share Posted April 26, 2014 Hello all! I got a login/register script. Its all working fine . When I click on Login, and didn't fill in the fields. It prints the error as it should. As you can see, its forced to print out under the menubar. But I want it into the Jumbotron. How can I do this?This code is defined on the top of the page to print the error: if($login_ok){ unset($row['salt']); unset($row['password']); $_SESSION['user'] = $row; header("Location: member.php"); die("Redirecting to: member.php"); } else{ print('<div class="alert alert-danger">Login failed!</div>'); $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); } But how can I print this somewhere else? I tried to move the code and I tried this on the place where it should print: if(!$login_ok){ print('<div class="alert alert-danger">Login failed!</div>'); $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); } Unfortunately that didn't worked Hopefully someone can help me out Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/ Share on other sites More sharing options...
Ch0cu3r Posted April 26, 2014 Share Posted April 26, 2014 If you want the login error message to appear in the large grey area, then you need to output the Login Failed error message inside the HTML for for the Jumbotron element. Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477342 Share on other sites More sharing options...
matthijs110 Posted April 26, 2014 Author Share Posted April 26, 2014 (edited) If you want the login error message to appear in the large grey area, then you need to output the Login Failed error message inside the HTML for for the Jumbotron element. So I put this code into the Jumotron? EDIT: I just tried this, but it results in: when I access the site This is my complete project if you want the full overview: https://github.com/matthijs110/Login-Project if($login_ok){ unset($row['salt']); unset($row['password']); $_SESSION['user'] = $row; header("Location: member.php"); die("Redirecting to: member.php"); } else{ print('<div class="alert alert-danger">Login failed!</div>'); $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); } Edited April 26, 2014 by matthijs110 Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477343 Share on other sites More sharing options...
Ch0cu3r Posted April 26, 2014 Share Posted April 26, 2014 Okay.. Which file is processing the login? index.php appears to be processing the login in two different places (on lines 4 - 36 and lines 121 - 148). Why? There is also a file called member.php which also processes the login. You should only have one instance for processing the login. Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477346 Share on other sites More sharing options...
matthijs110 Posted April 26, 2014 Author Share Posted April 26, 2014 Okay.. Which file is processing the login? index.php appears to be processing the login in two different places (on lines 4 - 36 and lines 121 - 148). Why? There is also a file called member.php which also processes the login. You should only have one instance for processing the login. Well I combined them. https://github.com/matthijs110/Login-Project Which file should have the processing of the login? Index.php? It has the login fields. Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477347 Share on other sites More sharing options...
Ch0cu3r Posted April 26, 2014 Share Posted April 26, 2014 (edited) Okay looking further at your code it does appear to be index.php is reprehensible for processing the login. So first delete the duplicated login code on lines 132 to to 161 in index.php. <?php if(isset($_POST["submit"])) { $user=$_POST['user']; $pass=$_POST['pass']; $con=mysql_connect('localhost','root','') or die(mysql_error()); mysql_select_db('User_Management') or die("cannot select DB"); $query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'"); $numrows=mysql_num_rows($query); if($numrows!=0) { while($row=mysql_fetch_assoc($query)) { $dbusername=$row['username']; $dbpassword=$row['password']; } if($user == $dbusername && $pass == $dbpassword) { session_start(); $_SESSION['sess_user']=$user; /* Redirect browser */ header("Location: member.php"); } else { echo "Please fill in the fields!"; } } else { echo "Invalid username or password!"; } } ?> Now delete the else on line 44 else{ print('<div class="alert alert-danger">Login failed!</div>'); $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); } And then change line 140 to <?php if(isset($login_ok) && $login_ok == false): ?> <div class="alert alert-danger">Login failed!</div> <?php endif; ?> <h1>Hello, world!</h1> Does the error message now display in the correct location? Edited April 26, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477350 Share on other sites More sharing options...
matthijs110 Posted April 26, 2014 Author Share Posted April 26, 2014 Okay looking further at your code it does appear to be index.php is reprehensible for processing the login. So first delete the duplicated login code on lines 132 to to 161 in index.php. <?php if(isset($_POST["submit"])) { $user=$_POST['user']; $pass=$_POST['pass']; $con=mysql_connect('localhost','root','') or die(mysql_error()); mysql_select_db('User_Management') or die("cannot select DB"); $query=mysql_query("SELECT * FROM login WHERE username='".$user."' AND password='".$pass."'"); $numrows=mysql_num_rows($query); if($numrows!=0) { while($row=mysql_fetch_assoc($query)) { $dbusername=$row['username']; $dbpassword=$row['password']; } if($user == $dbusername && $pass == $dbpassword) { session_start(); $_SESSION['sess_user']=$user; /* Redirect browser */ header("Location: member.php"); } else { echo "Please fill in the fields!"; } } else { echo "Invalid username or password!"; } } ?> Now delete the else on line 44 else{ print('<div class="alert alert-danger">Login failed!</div>'); $submitted_username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8'); } And then change line 140 to <?php if(isset($login_ok) && $login_ok == false): ?> <div class="alert alert-danger">Login failed!</div> <?php endif; ?> <h1>Hello, world!</h1> Does the error message now display in the correct location? Yes it does But now I found my next problem Registering. When I hit Register, it opens the forum, if I click on the Register button of that form, it results to: Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477351 Share on other sites More sharing options...
Ch0cu3r Posted April 26, 2014 Share Posted April 26, 2014 (edited) In register.php n line 104 the forms action should be set to register.php <form action="register.php" method="post"> Edited April 26, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477354 Share on other sites More sharing options...
matthijs110 Posted April 26, 2014 Author Share Posted April 26, 2014 In register.php n line 104 the forms action should be set to register.php <form action="register.php" method="post"> Okay, thats working now But when I clicked "Register", it gives me the following error: Notice: Undefined variable: db in /Applications/XAMPP/xamppfiles/htdocs/Login-Project-3/register.php on line 24 Fatal error: Call to a member function prepare() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/Login-Project-3/register.php on line 24 But it is defined. $query = " SELECT 1 FROM users WHERE username = :username "; $stmt = $db->prepare($query); Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477356 Share on other sites More sharing options...
Ch0cu3r Posted April 26, 2014 Share Posted April 26, 2014 No, $db is not defined. This line $stmt = $db->prepare($query); Is trying to use a method called prepare from an the $db object. The variable $db is in fact defined in config.php. So after line 1 in register.php you'd include that file include 'config.php'; Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477358 Share on other sites More sharing options...
matthijs110 Posted April 26, 2014 Author Share Posted April 26, 2014 No, $db is not defined. This line $stmt = $db->prepare($query); Is trying to use a method called prepare from an the $db object. The variable $db is in fact defined in config.php. So after line 1 in register.php you'd include that file include 'config.php'; I had that line before in the file. But I deleted it because of this error: Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477359 Share on other sites More sharing options...
Ch0cu3r Posted April 26, 2014 Share Posted April 26, 2014 Oh, I didnt see you were including register.php in index.php as part of a modal. What I recommend you to do then is to move the registration form out of register.php <form action="register.php" method="post"> <div class="input-group"> <span class="input-group-addon" style="min-width: 106px;"><i class="fa fa-user"></i> Username</span> <input type="text" class="form-control" name="user" placeholder="Username"> </div> <br> <div class="input-group"> <span class="input-group-addon" style="min-width: 106px; text-align:left;"><i class="fa fa-envelope"></i> Email</span> <input type="text" class="form-control" placeholder="name@example.com"> </div> <br> <div class="input-group"> <span class="input-group-addon" style="min-width: 106px;"><i class="fa fa-link"></i> Password</span> <input type="password" class="form-control" name="pass" placeholder="Password"> </div> <br> <button type="button" class="btn btn-info" data-dismiss="modal">Close</button> <input type="submit" class="btn btn-success" value="Register" /> </form> And put it into its own file, call this file register_form.php. Now in index.php on line 188 you'd include register_form.php instead of register.php <?php include"register_form.php"; ?> And then in place of the form in register.php you'd now include register_form.php Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477360 Share on other sites More sharing options...
matthijs110 Posted April 26, 2014 Author Share Posted April 26, 2014 Oh, I didnt see you were including register.php in index.php as part of a modal. What I recommend you to do then is to move the registration form out of register.php <form action="register.php" method="post"> <div class="input-group"> <span class="input-group-addon" style="min-width: 106px;"><i class="fa fa-user"></i> Username</span> <input type="text" class="form-control" name="user" placeholder="Username"> </div> <br> <div class="input-group"> <span class="input-group-addon" style="min-width: 106px; text-align:left;"><i class="fa fa-envelope"></i> Email</span> <input type="text" class="form-control" placeholder="name@example.com"> </div> <br> <div class="input-group"> <span class="input-group-addon" style="min-width: 106px;"><i class="fa fa-link"></i> Password</span> <input type="password" class="form-control" name="pass" placeholder="Password"> </div> <br> <button type="button" class="btn btn-info" data-dismiss="modal">Close</button> <input type="submit" class="btn btn-success" value="Register" /> </form> And put it into its own file, call this file register_form.php. Now in index.php on line 188 you'd include register_form.php instead of register.php <?php include"register_form.php"; ?> And then in place of the form in register.php you'd now include register_form.php For some reason when I hit Register, it says: Failed to run query: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'salt' in 'field list' It is in my SQL database Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477361 Share on other sites More sharing options...
Solution Ch0cu3r Posted April 26, 2014 Solution Share Posted April 26, 2014 You sure you're using the correct database table in your queries? In index.php for processing the login you're using the Members table but in register.php you're using the Users table in your queries? Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477363 Share on other sites More sharing options...
matthijs110 Posted April 26, 2014 Author Share Posted April 26, 2014 You sure you're using the correct database table in your queries? In index.php for processing the login you're using the Members table but in register.php you're using the Users table in your queries? Woops, my mistake o: Forgot to change it xD Thank you very much for your help. I really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/288036-print-error-on-different-part-of-the-website/#findComment-1477364 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.