0

我正在尝试使用数组从表单中回显错误。我将错误存储在一个数组中,然后我 print_r. 但是当我省略一些字段并提交时,我得到字符串“Array”显示为错误。

下面是我正在使用的 php 代码...

<?php
    if (isset($_POST['submit'])) { 
      $error = array();
      if (empty($_POST['request'])) { $error .= "<li>Prayer Request is Empty</li>";}
      if (empty($_POST['phone']))   { $error .= "<li>Please enter a valid phone Number</li>";}
      if (empty($_POST['response'])){ $error .= "<li>Please choose a way to contact you.</li>";}
      ?>
          <div class='Errors' >
          <?php print_r ($error); ?>
          </div>
4

2 回答 2

4

那么你期待$error = array()什么?

试试$error = "";吧。

于 2013-06-25T21:30:05.973 回答
1

尝试将 .= 更改为 [] =

<?php
  if (isset($_POST['submit'])) { 
  $error = array();
  if (empty($_POST['request'])) { $error[] = "<li>Prayer Request is Empty</li>";}
  if (empty($_POST['phone']))   { $error[] = "<li>Please enter a valid phone Number</li>";}
  if (empty($_POST['response'])){ $error[] = "<li>Please choose a way to contact you.</li>";}
?>
<div class='Errors' >
<?php print_r ($error); ?>
</div>
于 2013-06-25T21:33:53.890 回答