0

I want to show a popup alert when user uploaded a file I have 1 php file that contains php script and html form, when I click the submit button the file was saved. But the echo function that contains javascript alert doesnt pop up. Can anyone help me?

here's my code

<?php 
if(isset($_POST["btnSubmit"]))
{
    if($_FILES["photo"]["name"])
    {
        $name = $_FILES["photo"]["name"];
        $size = $_FILES["photo"]["size"];
        $type = $_FILES["photo"]["type"];
        if(!$_FILES["photo"]["error"])
        {
            move_uploaded_file($_FILES["photo"]["tmp_name"], "uploads/coba2.jpg");
            $msg = "Upload Berhasil\nFile Name: $name\nSize: $size bytes\nType: $type";
        }
        else
        {
            $msg = "Upload ERROR: " . $_FILES["photo"]["error"];
        }
    }
    else
    {
        $msg = "No File Uploaded";
    }
    if(isset($msg))
    {
        echo "<script type=\"text/javascript\">alert(".$msg.");</script>";
    }       
}?>


<!DOCTYPE html>
<html>
<head>
    <title>Coba Upload</title>
</head>
<body>
    <h3>Select File to Upload</h3><br>
    <form method="POST" action="#" enctype="multipart/form-data">
        <input type="file" name="photo" size="50"><br>
        <input type="submit" name="btnSubmit" value="Unggah Foto">  
    </form>
</body>
</html>

I'm wondering why the echo that contains javascript alert doesnt pop up. Thanks in advance.

7
  • Try to put inside the <head> tag Commented May 2, 2017 at 12:30
  • Do the script tags get appended? Are they visible in the HTML? Commented May 2, 2017 at 12:30
  • does the line if(isset($msg)) get executed Commented May 2, 2017 at 12:30
  • Does your code reach that point? Can you provide some kind of log statement or PHP echo to say you've gotten there, and it's definitely the JS that isn't working? Commented May 2, 2017 at 12:30
  • What is the resulting client-side code that gets rendered here? If your JavaScript isn't working, the first step is to actually look at your JavaScript. Commented May 2, 2017 at 12:30

1 Answer 1

2

You msg needs to be contained within quotes to alert correctly, so change:

{
  echo "<script type=\"text/javascript\">alert(".$msg.");</script>";
}  

To:

{
  echo '<script type="text/javascript">alert("'.$msg.'");</script>';
}  

Or, to continue using escaped quotes (urgh!):

{
  echo "<script type=\"text/javascript\">alert(\"".$msg."\");</script>"
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks it's working now if no files uploaded, but when I try to upload a file (it is saved in my directory) the $msg does not pop up.
@aldo-adhi, The context of the question has now changed. Can I suggest that you mark this question as complete and create a new question for help with the message contents. This will avoid confusion when people come to see the answer to your initial question.

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.