Advertisement
Guest User

Help with Pre-populating form PHP

a guest
Aug 3rd, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. Using information that a user has already provided in their registration (and which is now in an SQL database), when the user signs in and then clicks to go to a form, I want some of the form to already be pre-populated. The log-in form is as such:
  2.  
  3. <?php
  4. session_start();
  5. // check for required fields from the form
  6. if ((!isset($_POST['email'])) || (!isset($_POST['password']))) {
  7. header("Location: userlogin.html");
  8. exit;
  9. }
  10. //connect to server and select database
  11. $mysqli = mysqli_connect("localhost", "user", "password", "database")
  12. or die(mysqli_error());
  13. // use mysqli_real_escape_string to clean the input
  14.  
  15. $email = mysqli_real_escape_string($mysqli, $_POST['email']);
  16. $password = mysqli_real_escape_string($mysqli, $_POST['password']);
  17.  
  18. // Create and issue the query
  19. $sql = "SELECT username FROM frmak_form_1 WHERE
  20. email = '".$email."' AND
  21. password = '".$password."'";
  22.  
  23. $result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
  24.  
  25. //get the number of rows in the result set; should be 1 if a match.
  26. if (mysqli_num_rows($result) == 1 ) {
  27. // if authorized, get value of username
  28. while ($info = mysqli_fetch_array($result)) {
  29. $username = stripslashes($info['username']);
  30. }
  31.  
  32.  
  33.  
  34.  
  35. $_SESSION['user']= $info['username'];
  36. $_SESSION['login_until'] = time() + 60 * 60; // login expires in 1 hour
  37.  
  38. $display_block = "<p>Welcome, ".$username."!</p>
  39. <br /> <br />
  40. <a href='submitReview.php'>Click Here to Submit a Review!</a>";
  41. }
  42. else {
  43. header("Location: userlogin.html");
  44. exit;
  45. }
  46. mysqli_close($mysqli);
  47. ?>
  48. <!DOCTYPE html>
  49. <html>
  50. <head>
  51. <title>User Login REDIRECT!!!</title>
  52. </head>
  53.  
  54. <body>
  55. <?php echo $display_block; ?>
  56. </body>
  57.  
  58. </html>
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67. Then the "submitReview.php" file is:
  68.  
  69. <?php
  70. session_start();
  71. $s = &$_SESSION; // Login status is ON and has NOT yet expired
  72.  
  73. $isLoggedIn = isset($s['login_until']) && $s['login_until'] > time();
  74. if($isLoggedIn):
  75. $username = $s['user'];
  76.  
  77. $s['login_until'] = time() + 60 * 60; // extend login expiration to an hour from now.
  78.  
  79. else
  80. unset($s['login_until'], $['user']);
  81. $username = null;
  82.  
  83. endif;
  84. exit;
  85. }
  86. ?>
  87.  
  88. <!DOCTYPE html>
  89. <html>
  90. <head>
  91. </head>
  92. <body>
  93. <p>
  94. <label>username</label>
  95. <br />
  96. <input name="usrnm" id="usrnm" type="text" value="<?php echo $username; ?>"/>
  97. <br />
  98.  
  99. </p>
  100. </form>
  101. </body>
  102. </html>
  103.  
  104. In the example above, I'm only trying to display the user name in the form, because that's the only part that I want pre-populated as just a text field. The others are a radio button and a check box field, and I know those will take an even more complicated argument, so I'm just trying to make the simplest one work first.
  105.  
  106. If what I've done is not even remotely what should be done, could someone please just direct me toward a good tutorial for such things? I'm trying to teach myself PHP and MySQL, so sometimes it is a bit overwhelming. Thanks!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement