0

does anyone know how to return error array in a javascript alert box? The code below that I have doesn't do the trick right. Browser returns "Cannot use [] for reading". When using $error without the bracket, browser only returns the word "Array" in the alert box but the error message is not showing. Any help?

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

Updated to include the extent of my code:

  $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>';
          }
3
  • 1
    Update your question with the value of $errors Commented May 22, 2017 at 16:50
  • you need to first define the variable at the top of your code as an empty array: $errors = []; Commented May 22, 2017 at 17:07
  • Okay, I'm new to the site. I just added. hopefully, it's more clear now. . Commented May 22, 2017 at 17:10

4 Answers 4

2

Using implode() and entering new lines is most likely the best way.

Example:

<?php
$errors = [];

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

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

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

UPDATE:

Another error lies on your error message string syntax. Take a look at this line:

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

This should be re-written to:

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

As any variable inside double quotes "..." will be echoed out correctly. And a new line in the string should be \\n not \n.

Sign up to request clarification or add additional context in comments.

7 Comments

Not working, CodeGodie. Even worst, the Browser didn't detect any error even though I purposely planted error for testing.
@ubuntujavy have you defined your $errors variable as an empty array?
I just did, but it didn't work. I just updated my question to include the rest of my code. I think there's a syntax error somewhere in there but couldn't spot it.
Theres a lot going on in your code. You have variables that I dont know what they are like : $file_nameRFP, $extentionRFP, $valid_formatsRFP, etc.. any of those can be giving you different results.
Can you check my code again? I just updated my code to include variables sources.
|
1

You could transform your array in string with implode()

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

4 Comments

Doesn't work Rayann. Browser doesn't return any errors even though there is error planted for testing purpose.
My code is correct and tested, check my comment on your question.
I believe your code work, but for some reasons, it doesn't work on mine. I have updated my question to include the extent of the code. Can you check to spot what's wrong with it?
If no errors are being shown, the $errors is empty.
1

You'd have to create a single string representation of some sort. For example, you could "implode" the array using a delimiter. Something like:

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

This would just be a simple comma+space delimiter between all of the array elements. If you need something more complex or with more styling (in which case you probably don't want an alert() in the first place) then you could manually build any string you like from the contents of the array. You just need to output a string to the page, not an array directly.

10 Comments

Doesn't work David. Browser doesn't return any errors even though there is error planted for testing purpose.
@ubuntujavy: Can you provide a minimal and complete example of the problem? The code shown in the question doesn't guarantee that $errors isn't simply an empty array.
David, here is my error detection lines. 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."; } }
@ubuntujavy: Code belongs in the question, for obvious readability reasons.
Sorry. I'm new to the site and didn't realize I could edit code after 5 mins. I have updated my question to include error detection code.
|
0

Hopefully you will get exact output.Please try

alert(JSON.stringify($errors));

4 Comments

Thanks. I got syntax error. Where would I be adding your code into mine? echo '<script type="text/javascript">alert("Error: '.$errors[].'");</script>';
Yes echo '<script type="text/javascript">alert(JSON.stringify('.$errors.'));</script>';
Not working Waquas. I got Array to string conversion error.
Let me check please

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.