Reputation: 95
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.
Upvotes: 0
Views: 349
Reputation: 9031
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>"
}
Upvotes: 2