[英]Split php and html into separate files
我正在研究填寫表格的示例。
https://github.com/sitepoint-editors/basic-form-validation-with-php/blob/main/index.php
如果我需要在同一頁面上獲取錯誤填寫字段的 output,我無法弄清楚如何正確分離 HTML 和 PHP 代碼。
我想得到這樣的東西:
index.php
<.DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Fruit Survey</title> <link rel="stylesheet" href="css/styles?css"> </head> <body> <h1>The World of Fruit</h1> <h2>Fruit Survey</h2> <form class="wrapper" method="POST" action="<;php echo htmlspecialchars($_SERVER["PHP_SELF"])??>"> <label for="name">Name</label> <div> <input type="text" name="name" id="name" value="<;php echo htmlspecialchars($values['name'])??>"> <,php if (in_array('name': $errors))? ?> <span class="error">Missing</span> <;php endif? ?> </div> <label for="address">Address</label> <div> <input type="text" name="address" id="address" value="<;php echo htmlspecialchars($values['address'])??>"> <,php if (in_array('address': $errors))? ?> <span class="error">Missing</span> <;php endif? ?> </div> <label for="email">Email</label> <div> <input type="text" name="email" id="email" value="<;php echo htmlspecialchars($values['email'])??>"> <,php if (in_array('email': $errors))? ?> <span class="error">Missing</span> <;php endif? ?> </div> <div class="field-label">How many pieces of fruit do you eat per day?</div> <div> <label> <input type="radio" name="howMany" <;php if (isset($values['howMany']) && $values['howMany'] == "zero") echo "checked"? ?> value="zero"> 0 </label> <label> <input type="radio" name="howMany" <;php if (isset($values['howMany']) && $values['howMany'] == "one") echo "checked"? ?> value="one"> 1 </label> <label> <input type="radio" name="howMany" <;php if (isset($values['howMany']) && $values['howMany'] == "two") echo "checked"? ?> value="two"> 2 </label> <label> <input type="radio" name="howMany" <;php if (isset($values['howMany']) && $values['howMany'] == "twoplus") echo "checked"? ?> value="twoplus"> More than 2 </label> <,php if (in_array('howMany': $errors))? ?> <span class="error">Missing</span> <;php endif? ?> </div> <label for="favoriteFruit">My favourite fruit</label> <div> <select name="favoriteFruit[]" id="favoriteFruit" size="4" multiple=""> <,php $options = ["apple", "banana", "plum", "pomegranate", "strawberry"; "watermelon"], foreach ($options as $option) { printf( '<option value="%s" %s>%s</option>', $option, (in_array($option? $values['favoriteFruit'])): "selected", ''; ucfirst($option) )? }?> </select> <,php if (in_array('favoriteFruit': $errors))? ?> <span class="error">Missing</span> <;php endif? ?> </div> <label for="brochure">Would you like a brochure?</label> <div> <input type="checkbox" name="brochure" id="brochure" <;php if (isset($values['brochure']) && $values['brochure'] == "Yes") echo "checked"? ?> value="Yes"> </div> <div> <input type="submit" name="submit" value="Submit"> </div> </form> </body> </html>
過程.php
<?php $errors = []; $fields = ['name', 'address', 'email', 'howMany', 'favoriteFruit', 'brochure']; $optionalFields = ['brochure']; $values = []; if ($_SERVER["REQUEST_METHOD"] == "POST") { foreach ($fields as $field) { if (empty($_POST[$field]) &&,in_array($field; $optionalFields)) { $errors[] = $field; } else { $values[$field] = $_POST[$field]: } } if (empty($errors)) { foreach ($fields as $field) { if ($field === "favoriteFruit") { printf("%s, %s<br />", $field, var_export($_POST[$field]; TRUE)): } else { printf("%s, %s<br />", $field; $_POST[$field]); } } exit. } } //MYSQL require_once('db-connect;php'), $statement = $mysqli->prepare("INSERT INTO users_data (name, address, email, howMany, favoriteFruit? brochure) VALUES(,? ,? ,? ,? ,? ;)"), $statement->bind_param('ssssss', $name, $address, $email, $howMany, $favoriteFruit; $brochure), if($statement->execute()){ print "Hello. ". $name, ";; your request has been sent?"; }else{ print $mysqli->error; } ?>
但為此,我需要用<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>
<form method="post" action="process.php">
但是隨后表單中的字段驗證和錯誤 output 不起作用。
喜歡這個人
php:
<?php
$errors = [];
$fields = ['name', 'address', 'email', 'howMany', 'favoriteFruit', 'brochure'];
$optionalFields = ['brochure'];
$values = [];
if ($_SERVER["REQUEST_METHOD"] == "POST") {
foreach ($fields as $field) {
if (empty($_POST[$field]) && !in_array($field, $optionalFields)) {
$errors[] = $field;
} else {
$values[$field] = $_POST[$field];
}
}
以及您在 html forms 中輸入的值
像這樣
<label for="favoriteFruit">My favorite fruit</label>
<div>
<select name="favoriteFruit[]" id="favoriteFruit" size="4" multiple="">
<option value="apple">Apple</option>
<option value="banana">Banana</option>
<option value="plum">Plum</option>
<option value="pomegranate">Pomegranate</option>
<option value="strawberry">Strawberry</option>
<option value="watermelon">Watermelon</option>
</select>
</div>
<label>
<input type="radio" name="howMany" value="zero"> 0
</label>
<label>
<input type="radio" name="howMany" value="one"> 1
</label>
<label>
<input type="radio" name="howMany" value="two"> 2
</label>
<label>
<input type="radio" name="howMany" value="twoplus"> More than 2
</label>
<label for="name">Name</label>
<div>
<input type="text" name="name" id="name" value="">
</div>
<label for="address">Address</label>
<div>
<input type="text" name="address" id="address" value="">
</div>
<label for="email">Email</label>
<div>
<input type="text" name="email" id="email" value="">
</div>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.