1

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

6
  • ; missing after the alert Commented Apr 1, 2014 at 10:59
  • Yeah, its that simple. A look into the console would have shown you the problem ... JS statements need to be terminated with a ;, just like PHP. Commented Apr 1, 2014 at 10:59
  • its still not working even after adding ; Commented Apr 1, 2014 at 11:03
  • 1
    Can you check the output of this in the browser and see what is happening? Commented Apr 1, 2014 at 11:13
  • @KraneBird its executing uploading file to the desired folder but my problem is why is it not giving alerts... when am simply writing it with just <?php ?> its running and generating alerts but when i kept it inside script tag and it stop giving alert Commented Apr 1, 2014 at 11:22

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

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.