I am having problems submitting a form via ajax where without ajax it submits without any problems.
If I use this on it's own the image uploads correctly:
<form id="edit_image1_form" action="upload_file.php" method="post" enctype="multipart/form-data">
<img id="edit_image1" src="<?php echo $venue_image_upload_one; ?>" alt="" />
<input type="file" name="file" id="file" onchange="readURL1(this);" style="width:155px;"><br>
<input type="submit" id="upload_image1_submit" name="upload_image1_submit" value="Update Image">
</form>
But when I add this it give me an error and not upload is done:
$("#upload_image1_submit").click(function() {
var url = "upload_file.php";
$.ajax({
type: "POST",
url: url,
data: $("#edit_image1_form").serialize(), //form name here
success: function(data)
{
alert(data);
}
});
return false;
});
The error is Invalid File from here:
<?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
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file"; //HERE
}
?>
BUT my problem is that if I submit the form without ajax I will not get this error and the image I'm uploading all the time for testing is the same one.
Any ideas on what this could be ?