Sanddru

Untitled

Sep 14th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.   <head>
  4.     <meta charset="utf-8">
  5.     <style>
  6.       table {border-collapse: collapse; }
  7.       table, td {border: 1px solid black; }
  8.     </style>
  9.   </head>
  10.   <body>
  11.  <?php
  12.  
  13. //Get names of database to establish connection
  14. $servername = "localhost";
  15. $username = "root";
  16. $password = "";
  17. $dbname = "opslag";
  18.  
  19. // Create connection
  20. $conn = new mysqli($servername, $username, $password, $dbname);
  21. mysqli_set_charset($conn,"utf8");
  22. if ($conn->connect_error) {
  23.     die("Connection failed: " . $conn->connect_error);
  24. }
  25. ?>
  26.  
  27. <!-- Make two input fields to get username and password. POST is used to save the data that the user has entered. the form has an id so I can change
  28. its attributes later if needed -->
  29. Login
  30. <form id="myForm" method="POST">
  31. username: <input type="text" name="username" > <br>
  32. password: <input type="text" name="password" > <br>
  33.  
  34. <!-- submit button is created and given and id so I can refer to it elsehwere -->
  35. <input id=login type="submit" value=Submit>
  36. </form>
  37.  
  38. <?php
  39.  
  40. //check if text has been entered in username and password input-fields and then check if the given username and password is in the database-table "bruger"
  41. if(isset($_POST["username"])){
  42.   if (!empty($_POST["username"] && !empty($_POST["password"]))){
  43.     $username=$_POST["username"];
  44.     $password=$_POST["password"];
  45.     $myData="SELECT * from bruger where username='$username' and password='$password';";
  46.     $myResult=$conn->query($myData);
  47.    
  48.     //If the username and password is in the database...
  49.     if ($myResult->num_rows>0){
  50.     ?>
  51.     <script>
  52.  
  53.     // The JavaScript is used to change "myForm" action to "opslagsTavle.php" and then click the sumbit button so the action is activated
  54.       document.getElementById("myForm").action = "opslagsTavle.php";
  55.       document.getElementById("login").click();
  56.     </script>
  57.  
  58. <?php
  59.   }else{
  60.     echo "The entered username or passwor does not exist";
  61.   }
  62. }
  63. }
  64. $conn->close();
Add Comment
Please, Sign In to add comment