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.
asked May 2, 2017 at 12:28
Aldo Adhi
951 gold badge3 silver badges6 bronze badges
1 Answer 1
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>"
}
answered May 2, 2017 at 12:30
Alex
9,0612 gold badges29 silver badges44 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Aldo Adhi
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.
Alex
@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.
default
<head>tagif(isset($msg))get executed