php在javascript警告框中显示$ errors错误数组

时间:2017-05-22 16:36:42

标签: php

有没有人知道如何在javascript警告框中返回错误数组?我所拥有的代码并没有正确地解决问题。浏览器返回“无法使用[]进行阅读”。在没有括号的情况下使用$ error时,浏览器仅在警告框中返回单词“Array”,但未显示错误消息。有帮助吗?

echo '<script type="text/javascript">alert("Error: '.$errors[].'");</script>';

更新以包含我的代码范围:

  $errors = [];
      $target_dirRFP = ('accounts/' . $order_list . '/rfp/');
      $file_nameRFP = $_FILES['FileToUploadRFP']['name'];
      $file_sizeRFP = $_FILES['FileToUploadRFP']['size'];
      $extentionRFP = pathinfo($file_nameRFP, PATHINFO_EXTENSION);
      $tempRFP = ($_FILES["FileToUploadRFP"]["tmp_name"]);
      $target_fileRFP = ($target_dirRFP . $file_nameRFP);
      $valid_formatsRFP = array("pdf");

      $target_dirPSA = ('accounts/' . $order_list . '/psa/');
      $file_namePSA = $_FILES['FileToUploadPSA']['name'];
      $file_sizePSA = $_FILES['FileToUploadPSA']['size'];
      $extentionPSA = pathinfo($file_namePSA, PATHINFO_EXTENSION);
      $tempPSA = ($_FILES["FileToUploadPSA"]["tmp_name"]);
      $target_filePSA = ($target_dirPSA . $file_namePSA);
      $valid_formatsPSA = array("pdf");

          if(!empty($file_nameRFP)) {
            if(in_array($extentionRFP, $valid_formatsRFP) === false) {
              $errors[] = "The RFP you selected to upload is not allowed. Only PDF file is permitted.";
            }
            if (file_exists($target_fileRFP)) {
              $errors[] = "Attached RFP: \"'.$file_nameRFP.'\" already exists.\nPlease select another file to upload.";
            }
          }

          if(!empty($file_namePSA)) {
            if(in_array($extentionPSA, $valid_formatsPSA) === false ) {
              $errors[]="The PSA you selected to upload is not allowed. Only PDF file is permitted.";
            }
            if (file_exists($target_filePSA)) {
              $errors[] = "Attached PSA: \"'.$file_namePSA.'\" already exists.\nPlease select another file to upload.";
            }
          }

          if(empty($errors)==true) {
            move_uploaded_file($tempRFP, $target_fileRFP);
            move_uploaded_file($tempPSA, $target_filePSA);
            $stmt = $DB_CON_C->prepare("INSERT INTO `".$order_list."`
                    SET first_name = '$first_name', last_name = '$last_name'");
            $stmt->execute();
            echo '<script type="text/javascript">alert("Submitted.");</script>';
          } else {
              echo '<script type="text/javascript">alert("Error: ' . implode("; ", $errors) . '");</script>';
          }

4 个答案:

答案 0 :(得分:1)

您可以使用implode()

在字符串中转换数组
echo '<script type="text/javascript">alert("Error: ' . implode("; ", $errors) . '");</script>';

答案 1 :(得分:1)

您必须创建某种字符串表示形式。例如,你可以&#34;内爆&#34;使用分隔符的数组。类似的东西:

echo '<script type="text/javascript">alert("Error: ' . implode(', ', $errors) . '");</script>';

这只是所有数组元素之间的简单逗号+空格分隔符。如果您需要更复杂或更具样式的东西(在这种情况下,您可能首先不想要alert())那么您可以手动构建您喜欢的任何字符串数组的内容。您只需要将一个字符串输出到页面,而不是直接输出数组。

答案 2 :(得分:1)

使用implode()并输入新行很可能是最佳方法。

示例:

<?php
$errors = [];

if( validateFails() ){
    $errors[] = "some error found";
}

if( failAgain() ){
    $errors[] = "some other error";
}

echo '<script>alert("'.implode("\\n", $errors).'");</script>';

<强>更新

另一个错误在于您的错误消息字符串语法。看看这一行:

$errors[] = "Attached PSA: \"'.$file_namePSA.'\" already exists.\nPlease select another file to upload.";

这应该重写为:

$errors[] = "Attached PSA: $file_namePSA already exists.\\nPlease select another file to upload.";

因为双引号"..."内的任何变量都会正确回显。字符串中的新行应为\\n而不是\n

答案 3 :(得分:0)

希望您能得到准确的输出。请尝试

alert(JSON.stringify($errors));