Skip to Content
PHPCODE
Image upload without refreshing page using php jquery
jqueryphp code / April 18, 2018
How might I transfer pictures with ajax/jQuery and PHP without reviving the page?

    I am endeavoring to enable clients to uploaAre you searching for picture transfer and review d pictures and show them in agreement

   without invigorating page utilizing jQuery and PHP? You’re here at ideal place. Here is an extremely basic content used to transfer picture record without page revive utilizing PHP and jQuery.

    index.php: This is the fundamental document that contains straightforward PHP code to influence database association, to introduce session and HTML code with record transfer FORM. The record additionally incorporate jQuery libraray, jQuery picture transfer document and CSS document.

    Here in index.php, I have additionally called image_upload.php on shape activity that handle picture transfer usefulness without page revive when peruse picture document with the assistance of jQuery upload.js.<div class=“upload_container”>
<br clear=“all” />
<center>
<div style=width:350px align=“center”>
<div id=‘preview’></div>
<form id=“image_upload_form” method=“post” enctype=“multipart/form-data”action=‘image_upload.php’ autocomplete=“off”>
<div class=“browse_text”>Browse Image File:</div>
<div class=“file_input_container”>
<div class=“upload_button”><input type=“file” name=“photo” id=“photo” class=“file_input” /></div>
</div><br clear=“all”>
</form>
</div>
</center>
<br clear=“all” />
</div>


This is an example database outline for Users. The table clients will contain client subtle elements client, pass, email and profile_photo.


CREATE TABLE IF NOT EXISTS `users` (
`uid` int(11) NOT NULL AUTO_INCREMENT,
`user` varchar(255) DEFAULT NULL,
`pass` varchar(100) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`profile_photo` varchar(200) DEFAULT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `username` (`user`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;



The document image_upload.php is a noteworthy PHP content that handles picture transfer usefulness to the server and furthermore refresh database table with transferred picture points of interest


<?php
$path = “uploads/”;
$valid_file_formats = array(“jpg”, “png”, “gif”, “bmp”,“jpeg”);
if(isset($_POST) and $_SERVER[‘REQUEST_METHOD’] == “POST”)
{
$name = $_FILES[‘photo’][‘name’];
$size = $_FILES[‘photo’][‘size’];
if(strlen($name)) {
list($txt, $ext) = explode(“.”, $name);
if(in_array($ext,$valid_file_formats)) {
if($size<(1024*1024)) {
$image_name = time().$session_id.“.”.$ext;
$tmp = $_FILES[‘photo’][‘tmp_name’];
if(move_uploaded_file($tmp, $path.$image_name)){
mysql_query(“UPDATE users SET profile_photo=’$image_name’ WHERE uid=’$session_id'”);
echo “<img src=’uploads/”.$image_name.“‘ class=’preview’>”;
}
else
echo “Image Upload failed”;
}
else
echo “Image file size maximum 1 MB”;
}
else
echo “Invalid file format”;
}
else
echo “Please select image”;
exit;
}
?>


 contains code to submit frame when picture document upload.js transferred to achieve picture transfer usefulness with image_upload.php

$(document).ready(function() {
$(‘#photo’).live(‘change’, function() {
$(“#preview”).html();
$(“#preview”).html(‘<img src=”loader.gif” alt=”Uploading….”/>’);
$(“#image_upload_form”).ajaxForm({
target: ‘#preview’
}).submit();
});
});



DEMO DOWNALOD 

please comment video created me this post

PHPCODE © 2024