我有以下PHP代码独立运行,但问题是它也在表单加载上运行 所以我把它放在标签里面
<script>
$(document).ready(function() {
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo '<script>alert(" Logo with this name already exists.")</script>';
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo '<script>alert("project logo uploaded successfully")</script>';
}
}
}
else
{
echo '<script> alert("Invalid file")</script>';
}
?>
});
</script>
但问题是它正在执行上传文件但不生成警报 请帮忙!!
答案 0 :(得分:2)
代码的输出;
<script>
$(document).ready(function() {
<script> alert("Invalid file")</script>
});
</script>
应该是;
<script>
$(document).ready(function() {
alert("Invalid file");
});
</script>
答案 1 :(得分:1)
您在另一个<script>
元素中不能拥有<script>
元素。
通过validator运行生成的HTML。