user3484692
user3484692

Reputation: 19

php code not running properly under script tag

i have the following php code its running properly independently, but the problem is it runs on form load as well so i kept it inside tag

       <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>

but the problem is this its executing uploading the the file but not generating alerts please help!!

Upvotes: 1

Views: 128

Answers (2)

Nisse Engstr&#246;m
Nisse Engstr&#246;m

Reputation: 4752

You can't have a <script> element inside another <script> element. Run the generated HTML through a validator.

Upvotes: 1

Serhat
Serhat

Reputation: 396

the output of your code;

 <script>
    $(document).ready(function() {
 <script> alert("Invalid file")</script>
    });
    </script>

should be;

<script>
        $(document).ready(function() {
     alert("Invalid file");
        });
        </script>

Upvotes: 2

Related Questions