问题描述
所以我试图通过在第一个 PHP 文件中使用 $_GET['title'] 从 URL 获取标题,但我无法在第二个文件中获取该文件。
网址:
<?PHP
include_once 'header.PHP';
$id2 = MysqLi_real_escape_string($conn,$_GET['bookid']);
$title2 = MysqLi_real_escape_string($conn,$_GET['title']);
?>
<section class="neworder-form">
<h2>Order</h2>
<div class="neworder-form-form">
<form action="neworder.inc.PHP" method="post">
<table>
<tr>
<td>Book ID:</td>
<td>
<input type="text" disabled="disabled" name="bookid2" value="<?= $id2 ?>">
</td>
</tr>
<tr>
<td>Book Title: </td>
<td>
<input type="text" disabled="disabled" name="title2" value="<?= $title2 ?>">
</td>
</tr>
<tr>
<td>Username: </td>
<td>
<input type="text" name="uid2" placeholder="Username...">
</td>
</tr>
<tr>
<td>Comfirmed Book ID: </td>
<td>
<input type="text" name="id2" placeholder="Please enter the Book ID....">
</td>
</tr>
</table>
<button type="submit" name="submit2">Order</button>
</form>
</div>
<?PHP
// Error messages
if (isset($_GET["error"])) {
if ($_GET["error"] == "emptyinput2") {
echo "<p>Fill in all fields!</p>";
}
else if ($_GET["error"] == "usernaMetaken2") {
echo "<p>Username already taken!</p>";
}
}
?>
</section>
第二个文件:
<?PHP
if (isset($_POST["submit2"])) {
// First we get the form data from the URL
$uid2 = $_POST["uid2"];
$id2 = $_POST["id2"];
$title2 = $_POST["title2"];
// Then we run a bunch of error handlers to catch any user mistakes we can (you can add more than I did)
// These functions can be found in functions.inc.PHP
require_once "dbh.inc.PHP";
require_once 'functions2.inc.PHP';
// Left inputs empty
// We set the functions "!== false" since "=== true" has a risk of giving us the wrong outcome
if (emptyInputOrder2($uid2,$id2) !== false) {
header("location: ../neworder.PHP?error=emptyinput&bookid=$id2&title=$title2");
exit();
}
// Is the username exists
if (uidExists2($conn,$uid2) !== true) {
header("location: ../neworder.PHP?error=undefineuser");
exit();
}
// If we get to here,it means there are no user errors
// Now we insert the user into the database
createuser($conn,$uid2,$id2);
} else {
header("location: ../neworder.PHP");
exit();
}
解决方法
- 输入字段已禁用,禁用的输入不会发布。
- 替换 $title2 = $_POST[""]; $title2 = $_POST["title2"];