简体   繁体   English

将 php 和 html 拆分为单独的文件

[英]Split php and html into separate files

I am studying an example of filling out a form.我正在研究填写表格的示例。

https://github.com/sitepoint-editors/basic-form-validation-with-php/blob/main/index.php https://github.com/sitepoint-editors/basic-form-validation-with-php/blob/main/index.php

And I can't figure out how to properly separate HTML and PHP code if I need to get the output of incorrectly filled fields on the same page.如果我需要在同一页面上获取错误填写字段的 output,我无法弄清楚如何正确分离 HTML 和 PHP 代码。

I want to get something like that:我想得到这样的东西:

index.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>

process.php过程.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; } ?>

But for that I need to replace <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> with <form method="post" action="process.php"> But then field validation and error output in the form does not work.但为此,我需要用<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> <form method="post" action="process.php">但是随后表单中的字段验证和错误 output 不起作用。

like this man喜欢这个人

php: 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];
        }
    }


  

And what ever you put as a value in html forms以及您在 html forms 中输入的值

like this像这样

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM