使用用戶已經在其註冊中提供的信息(當前位於SQL數據庫中),當用戶登錄並單擊以轉到表單時,我想要一些的表單已被預先填充。所述登錄表格是這樣:使用Cookie預填充PHP表格
<?php
session_start();
// check for required fields from the form
if ((!isset($_POST['email'])) || (!isset($_POST['password']))) {
header("Location: userlogin.html");
exit;
}
//connect to server and select database
$mysqli = mysqli_connect("localhost", "user", "password", "database")
or die(mysqli_error());
// use mysqli_real_escape_string to clean the input
$email = mysqli_real_escape_string($mysqli, $_POST['email']);
$password = mysqli_real_escape_string($mysqli, $_POST['password']);
// Create and issue the query
$sql = "SELECT username FROM frmak_form_1 WHERE
email = '".$email."' AND
password = '".$password."'";
$result = mysqli_query($mysqli, $sql) or die(mysqli_error($mysqli));
//get the number of rows in the result set; should be 1 if a match.
if (mysqli_num_rows($result) == 1) {
// if authorized, get value of username
while ($info = mysqli_fetch_array($result)) {
$username = stripslashes($info['username']);
}
$_SESSION['user']= $info['username'];
$_SESSION['login_until'] = time() + 60 * 60; // login expires in 1 hour
$display_block = "<p>Welcome, ".$username."!</p>
<br /> <br />
<a href='submitReview.php'>Click Here to Submit a Review!</a>";
}
else {
header("Location: userlogin.html");
exit;
}
mysqli_close($mysqli);
?>
<!DOCTYPE html>
<html>
<head>
<title>User Login REDIRECT!!!</title>
</head>
<body>
<?php echo $display_block; ?>
</body>
</html>
那麼「submitReview.php」文件是:
<?php
session_start();
$s = &$_SESSION; // Login status is ON and has NOT yet expired
$isLoggedIn = isset($s['login_until']) && $s['login_until'] > time();
if($isLoggedIn):
$username = $s['user'];
$s['login_until'] = time() + 60 * 60; // extend login expiration to an hour from now.
else
unset($s['login_until'], $['user']);
$username = null;
endif;
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>
<label>username</label>
<br />
<input name="usrnm" id="usrnm" type="text" value="<?php echo $username; ?>"/>
<br />
</p>
</form>
</body>
</html>
在上面的例子中,我只試圖顯示在用戶名形式,因爲這是我希望預填充爲文本字段的唯一部分。其他人是一個單選按鈕和一個複選框字段,我知道這些將需要更復雜的論證,所以我只是試圖讓最簡單的一個首先工作。
如果我所做的甚至不是遠程應該做什麼,有人請指導我爲這樣的事情做一個好的教程嗎?我試圖教自己PHP和MySQL,所以有時候它有點令人難以置信。謝謝!
如果你錯過了它,你沒有在第一個代碼塊中修改你網站的名字。 – 2017-07-29 23:54:37
我更新了代碼以反映下面答案中的建議......仍然無法正常工作。我很確定我有一些語法錯誤,{}; ():等等......正如我所說的,我試圖自己學習這一切,所以請耐心等待...... –