我在頁面上有一系列與影像上傳相關的表單。我有一些 PHP 驗證發生,我希望在驗證失敗的單個表單實體的頂部輸出錯誤訊息。
我已經注意到這里的其他問題與此問題有關,但它們是隱藏表單元素的名稱本質上是硬編碼的實體。
下面代碼中的站點用戶最多可以上傳 10 張影像,因此頁面上最多可以有 10 個表單實體,這些實體通過while回圈輸出。
在下面的代碼中,我在hidden輸入元素中有一個檔案名變數(這個檔案名是在最初通過 Imagick PHP 庫上傳影像時分配的)。在每個表單的頂部,我有一個foreach回圈回顯失敗的驗證訊息(在下面的代碼中我只包含一個驗證),因此希望錯誤訊息應用于具有錯誤的特定表單實體。
以下是提交的一些資訊的示例,包括示例 PHP 驗證:
<?php
if(isset($_POST['upload-submit'])) {
$image_title = $_POST['image-title'];
$image_tags = $_POST['image-tags'];
$form_filename = $_POST['image-filename']; // hidden form element
$image_title = filter_var($image_title, FILTER_SANITIZE_STRING);
$image_tags = filter_var($image_tags, FILTER_SANITIZE_STRING);
$form_filename = filter_var($form_filename, FILTER_SANITIZE_STRING);
// example of form validation
if(empty(trim($image_title))){
$error[] = "Image Title cannot be blank";
}
if (!isset($error)) {
try {
$sql = "UPDATE imageposts SET
image_title = :image_title,
image_tags = :image_tags
WHERE filename = :filename";
$stmt = $connection->prepare($sql);
$stmt->execute([
':image_title' => $image_title,
':image_tags' => $image_tags,
':filename' => $form_filename
]);
} catch (PDOException $e) {
echo "Error: " . $e->getMessage(); // this is for PDO errors not validation errors
}
} else {
// give values an empty string to avoid an error being thrown before form submission if empty
$image_title = $image_tags = "";
}
}
?>
表單實體如何輸出到頁面的簡化版本:
<?php
// $user_id is assigned via a $_SESSION when user logs in
$stmt = $connection->prepare("SELECT * FROM imageposts WHERE user_id = :user_id");
$stmt->execute([
':user_id' => $user_id
]);
while ($row = $stmt->fetch()) {
$db_image_filename = escape($row['filename']); // outputted into the value attribute of the hidden input element
?>
<form method="post" enctype="multipart/form-data">
<?php
// -- echo error messages at top of the form
if(isset($error)) {
foreach($error as $msg) {
echo "<p>* ERROR: {$msg}</p>";
}
}
?>
<div>
<div class="form-row">
<label for="upload-details-title">Image Title</label>
<input id="upload-details-title" type="text" name="image-title">
</div>
<div class="form-row">
<label for="upload-details-tags">Comma Separated Image Tags</label>
<textarea id="upload-details-tags" type="text" name="image-tags"></textarea>
</div>
<div class="form-row">
<button name="upload-submit">COMPLETE UPLOAD</button>
</div>
<div class="form-row">
<!-- hidden form input -->
<input type="hidden" name="image-filename" value="<?php echo $db_image_filename; ?>">
</div>
</div>
</form>
<?php } ?>
如何獲取它以便出現錯誤的表單實體是唯一顯示錯誤訊息的實體?
uj5u.com熱心網友回復:
您首先需要一些東西來識別您的表單實體,并且您需要將它與其余欄位一起提交,以便您擁有提交來自表單的資訊,在您所在的位置可用做你的驗證。
由于顯然影像檔案名本身可用于識別它在此處的形式,因此您無需創建和發送任何額外內容。只需$form_filename === $db_image_filename在while回圈內輸出表單內的錯誤訊息之前檢查是否。
你可以把它和檢查是否甚至設定 $errors 放在同一個如果:
<form method="post" enctype="multipart/form-data">
<?php
// -- echo error messages at top of the form
if($form_filename === $db_image_filename && isset($error)) {
foreach($error as $msg) {
echo "<p>* ERROR: {$msg}</p>";
}
}
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/327191.html
上一篇:嘗試讀取陣列上的屬性“名稱”
