我试图建立一个网页,允许用户输入代码(数字和字母只)。如果代码是“赢家”,它会将他们重定向到我的网站上的“赢家”页面。否则,它会将它们重定向到“丢失”页面。我是新手,但我正在努力学习!
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();
}
?>
1条答案
按热度按时间gdrx4gfi1#
删除
onclick="window.location.href = 'results-page.html';"
和修改<input type="hidden" name="code" value="1" />
至<input type="hidden" name="questionID" value="1" />
html格式:将html文件保存到form.html
现在修改phpfile.php
添加
header("Location: winner.html");
如果代码正确添加
header("Location: losing.html");
如果代码不正确PHP:
创建html winner.html和losing.html
winner.html:
Congrats, you've entered a correct code
正在丢失.html:Your code did not win. Please try again.