I've been trying to use this tutorial at http://tuts.wtfdiary.com/2012/06/file-upload-in-php-without-refreshing.html and I haven't had any luck with implimenting it into my own site. I've even coppied the script word for word and still nothing - Are there any reasons why this may not work? I've even changed the .live to .on and changed the JQuery version. I'm wondering if it's anything to do with submit? Thank you
Here is the code I'm using:
<div id="formArea" class="userProfileFormWidth">
<script type="text/javascript" >
$(document).ready(function() {
$('#photoimg').live('change', function()
{
$("#preview").html('');
$("#current").hide();
$("#preview").html('<img src="ajax-loader.gif" alt="Uploading file...."/>');
$("#imageform").ajaxForm({
target: '#preview'
}).submit();
});
});
</script>
<form id="imageform" method="post" enctype="multipart/form-data" action='update-user-profile.php'>
Upload image from your computer: <input type="file" name="photoimg" id="photoimg" /><br><br/>
</form>
<div id='preview'></div>
</div>
update-user-profile.php
<?php
session_start();
include("php/db-connect.php");
$get_user_sql = "SELECT * FROM members WHERE username = '$user_username'";
$get_user_res = mysqli_query($con, $get_user_sql);
while($user = mysqli_fetch_array($get_user_res)){
$user_id = $user['id'];
}
$path = "uploads/";
$valid_formats = array("jpg", "png", "gif");
if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST")
{
$name = $_FILES['photoimg']['name'];
$size = $_FILES['photoimg']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(1024*1024))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['photoimg']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
$upload_avatar_sql = "UPDATE profile_members SET avatar='$actual_image_name' WHERE id='$user_id'";
$upload_avatar_res = mysqli_query($con, $upload_avatar_sql)or die(mysqli_error());
echo "<img src='uploads/".$actual_image_name."' class='preview'>";
}
else
echo "failed";
}
else
echo "Image file size max 1024k";
}
else
echo "Invalid file format..";
}
else
echo "No file selected";
exit;
}
?>