我正在尝试建立一个网页,允许用户输入代码(仅数字和字母)。根据代码是否为“优胜者”,它将代码重定向到我网站上的“优胜者”页面。否则,它将重定向到“丢失”页面。我是新手,但我正在尝试学习!
HTML:
<h1>Break Open Your Code</h1>
<form target="_blank" action="phpfile.php" method="POST">
<input type="hidden" name="code" value="1" />
<label>Enter Your Code Here</label>
<input type="text" name="answer" />
<input onclick="window.location.href = 'results-page.html';" type="submit" value="Check your Code" />
</form>
PHP:
<?php
// Connect to your MySQL database
$dbhst = "localhost";
$dbnme = "mydatabase";
$bdusr = "myusername";
$dbpws = "mypassword";
// Using PDO to connect
$conn = new PDO('mysql:host='.$dbhst.';dbname='.$dbnme, $bdusr, $dbpws);
// Getting variables
$answer = $_POST['answer'];
$questionID = $_POST['questionID'];
// Comparing answers
try {
$stmt = $conn->prepare('SELECT * FROM table_with_answers WHERE question='" . $questionID . "' and answer='". $answer . "' LIMIT 0,1');
$stmt->execute();
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
echo 'Congrats, you've entered a correct code';
// Do Something Else
}
} else {
echo 'Your code did not win. Please try again.';
exit;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
去掉 onclick="window.location.href = 'results-page.html';"
并修改 <input type="hidden" name="code" value="1" />
至 <input type="hidden" name="questionID" value="1" />
HTML:
<h1>Break Open Your Code</h1>
<form target="_blank" action="phpfile.php" method="POST">
<input type="hidden" name="questionID" value="1" />
<label>Enter Your Code Here</label>
<input type="text" name="answer" />
<input type="submit" value="Check your Code" />
</form>
将html文件保存到form.html
现在修改phpfile.php
header("Location: winner.html");
如果代码正确,则添加
header("Location: losing.html");
如果代码不正确,则添加
PHP的:
<?php
// Connect to your MySQL database
$dbhst = "localhost";
$dbnme = "mydatabase";
$bdusr = "myusername";
$dbpws = "mypassword";
// Using PDO to connect
$conn = new PDO('mysql:host='.$dbhst.';dbname='.$dbnme, $bdusr, $dbpws);
// Getting variables
$answer = $_POST['answer'];
$questionID = $_POST['questionID'];
// Comparing answers
try {
$stmt = $conn->prepare("SELECT * FROM table_with_answers WHERE question='" . $questionID . "' and answer='". $answer . "' LIMIT 0,1");
$stmt->execute();
$result = $stmt->fetchAll();
if ( count($result) ) {
foreach($result as $row) {
// echo 'Congrats, you've entered a correct code';
header("Location: winner.html");
}
} else {
// echo 'Your code did not win. Please try again.';
header("Location: losing.html");
exit;
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
创建html Winner.html和Lost.html
winner.html: Congrats, you've entered a correct code
missing.html: Your code did not win. Please try again.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句