是否可以缩短此PHP代码?

时间:2017-11-10 19:22:19

标签: php

我需要有关此代码的帮助。我怎样才能缩短它?我想我怎样才能创建一个带有名称的数组,例如错误修改代码而没有那么多If语句。我仍然无法解决它。我是PHP的初学者     

$error1 = $error2 = $error3 = $error4 = $error5 = $error6 = $error7 = $error8 = ""; 

if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["fname"])) {
  $error1 = "fill in fname";
}  
if (empty($_POST["lname"])) {
  $error2 = "fill in lname";
} 
if (empty($_POST["street"])) {
  $error3 = "fill in street";
}  
if (empty($_POST["city"])) {
  $error4 = "fill in city";
} 
if (empty($_POST["postcode"])) {
  $error5 = "fill in postcode";
} 
if (empty($_POST["country"])) {
  $error6 = "fill in country";
} 
if (empty($_POST["email"])) {
  $error7 = "fill in email";
} 
if (empty($_POST["phone"])) {
  $error8 = "fill in phone";
} 
if (empty($_POST["fname"]) ||
    empty($_POST["lname"]) ||
    empty($_POST["street"]) ||
    empty($_POST["city"]) ||
    empty($_POST["postcode"]) ||
    empty($_POST["country"]) ||
    empty($_POST["email"]) ||
    empty($_POST["phone"])) {
} else {
  $form_items = $_POST;
    $document = new DOMDocument("1.0", "utf-8"); 
    if (is_file('form_data.xml')) 
    {
        $document->load("form_data.xml"); 
        $xmlko = $document->getElementsByTagName('xml')->item(0); 
    }
    else 
    {
        $xmlko = $document->createElement("xml"); 
        $document->appendChild($xmlko); 
        $xmlko = $document->getElementsByTagName('xml')->item(0); 
    }
            $form = $document->createElement("form"); 
                $new_user = $document->createElement("user"); 
                foreach ($form_items as $ite => $val) { 
                    $this_user = $document->createElement($ite, $val); 
                    $new_user->appendChild($this_user); 
                }
               $form->appendChild($new_user); 
           $xmlko->appendChild($form); 
    $document->save("form_data.xml"); 

  }
}

?>

这是HTML代码。一切都在一个PHP文件中。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>form</title>
    <link rel="stylesheet" href="CSS/styly.css">
  </head>
  <body>
    <h1>FORM</h1>
    <form action="" method="post">
      <p>FName:</p>
      <input type="text" name="fname"/><span class="error"><?php echo $error1?></span>
      <p>LName:</p>
      <input type="text" name="lname"/><span class="error"><?php echo $error2?></span>
      <p>Street:</p>
      <input type="text" name="street"/><span class="error"><?php echo $error3?></span>
      <p>City:</p>
      <input type="text" name="city"/><span class="error"><?php echo $error4?></span>
      <p>Postcode:</p>
      <input type="text" name="postcode"/><span class="error"><?php echo $error5?></span>
      <p>Country:</p>
      <input type="text" name="country"/><span class="error"><?php echo $error6?></span>
      <p>E-mail:</p>
      <input type="text" name="email"/><span class="error"><?php echo $error7?></span>
      <p>Phone:</p>
      <input type="text" name="phone"/><span class="error"><?php echo $error8?></span> 
      <input type="submit" value="SUBMIT"/> 
    </form>

   <?php 

      $document = new DOMDocument("1.0", "utf-8");
      if (is_file('form_data.xml')) {
        $document->load("form_data.xml");
        $users = $document->getElementsByTagName("user");

        echo "<table>";

            echo '<tr>';
            foreach($users->item(0)->childNodes as $data) {
                echo '<th>'.$data->nodeName."</th>";
            }
            echo '</tr>';


            foreach($users as $user){
                echo '<tr>';
                foreach($user->childNodes as $data) {
                    echo '<td>'.$data->nodeValue."</td>";
                }
                echo '</tr>';
            }

        echo "</table>";
      }

   ?>

  </body>
</html>

来自html表单的所有数据都会插入到表单下方的表格中。 提前致谢!

2 个答案:

答案 0 :(得分:0)

这种方式会检查是否设置了帖子值,然后你可以像表格底部的一些网站一样添加一般的错误信息。

if(!isset($_POST['fname'])|| !isset($_POST['lname']) || !isset($_POST['street']) || !isset($_POST['city']) || !isset($_POST['postcode']) || !isset($_POST['country']) || !isset($_POST['email']) || !isset($_POST['email']) || !isset($_POST['phone']){
    $error = "All values must be filled in.";
}

HTML

<?php echo $error; ?>
<input type="submit" value="SUBMIT"/> 

更好的方法是遍历post数组并将所有HTML元素添加到一个post元素数组中:

<input type="text" name="input[]" />
<input type="text" name="input[]" />

答案 1 :(得分:0)

这段代码怎么样?

$requiredFields = ['fname', 'lname', 'street', 'city', 'postcode', 'country', 'email', 'phone'];

$errors = [];

foreach ($_POST as $key => $val) {
    if (!in_array($key, $requiredFields)) {
        continue;
    }

    $errors[$key] = 'fill in '.$key;
}

if (empty($errors)) {
    $form_items = $_POST;
    $document = new DOMDocument("1.0", "utf-8"); 
    if (is_file('form_data.xml')) 
    {
        $document->load("form_data.xml"); 
        $xmlko = $document->getElementsByTagName('xml')->item(0); 
    }
    else 
    {
        $xmlko = $document->createElement("xml"); 
        $document->appendChild($xmlko); 
        $xmlko = $document->getElementsByTagName('xml')->item(0); 
    }
            $form = $document->createElement("form"); 
                $new_user = $document->createElement("user"); 
                foreach ($form_items as $ite => $val) { 
                    $this_user = $document->createElement($ite, $val); 
                    $new_user->appendChild($this_user); 
                }
               $form->appendChild($new_user); 
           $xmlko->appendChild($form); 
    $document->save("form_data.xml"); 

  }
}