Friday, 5 February 2016

how to pass form data with image using ajax to php


step 1:

Create table record 
( name varchar(200),

  contact varchar(200),

 image_file  varchar(200)

step 2:
(1)write    code   for    upload.html      file  :
<!doctype html>
<html lang="en">
<title>Image Preview before Upload Demo - BY OM SIR</title>
  <script type="text/javascript" src=""></script>
  <script type="text/javascript" src="//"></script>
    <script type="text/javascript" src=""></script>
  <link rel="stylesheet" href="" type="text/css" />
  <link rel="stylesheet" href="//" type="text/css" />
<script type="text/javascript">
$(document).ready(function (event) {
// triggered when the form submitted
$("#selected_image").on('submit',(function(event) {
// URL to move the uploaded image file to server
url: "upload.php",
// Request type
type: "POST",
// To send the full form data
data: new FormData(this),
contentType: false,    
// UI response after the file upload
success: function(data)

// Triggered when the image changed (browse)
$(function() {
$("#image_file").change(function(event) {
if(-1!=$.inArray($("#image_file")[0].files[0].type, ["image/jpeg","image/png","image/jpg","image/gif"])){        
var populateImg = new FileReader();
populateImg.onload = previewImg;

// Function to show the image as a preview
function previewImg(event) {
$('.img').css("display", "block");
$('#img_preview').attr('width', '300px');
$('#img_preview').attr('height', '250px');
<div class="container">
  <div class="row">
    <div class="span12">
      <div class="head">
        <div class="row-fluid">
            <div class="span12">
                <div class="span6">
                    <h1 class="muted"><a href="">&nbsp;&nbsp;&nbsp;Image Preview before Upload Demo | BY OM SIR</a></h1>
            <div class="jumbotron">
  <div class="container">

 <div class="row">
  <div class="col-sm-8">
  <label>Select Image to Upload</label>
    <div class="row">
      <div class="col-sm-6">
                  <form id="selected_image" action="" method="post" enctype="multipart/form-data">
                  <input type="file" name="image_file" id="image_file" class="btn btn-sm btn-primary" style="width:250px;"/>
                  Name<input type=text name=name  size=20>
                  Contact<input type=text name=contact  size=20>
      <div class="col-sm-6"><input type="submit" value="Upload" class="btn btn-sm btn-success" style="width:250px;"/>
  <div class="col-sm-4">
<div class="img"><img id="img_preview" /></div>
<div id="upload_status"></div>

(2)write    code  upload.php   file   :

$uploadedImgFileExtension = $_FILES["image_file"]["type"];
$uploadedImgFileSize = $_FILES["image_file"]["size"];
// checking the image file types and size
if(( ($uploadedImgFileExtension=="image/png") || ($uploadedImgFileExtension=="image/gif") || ($uploadedImgFileExtension=="image/jpg") || ($uploadedImgFileExtension=="image/jpeg")) ) {
// Name of the uploaded file
$uploadedImgFileName =$_FILES['image_file']['name'];
if (file_exists("upload/" . $uploadedImgFileName)) {
echo $uploadedImgFileName . " Exists already ";
// Temporary path to store the uploaded image
$tempPath = $_FILES['image_file']['tmp_name'];
// destination folder name where the uploaded image need to be moved
$moveFileTo = "upload/";
$destFullPath = $moveFileTo.$uploadedImgFileName;
// Moving the uploaded image file
$isMoved = move_uploaded_file($tempPath,$destFullPath) ;
if ($isMoved == true) {
                $sql="insert into record(name,contact,image_file)values('$name','$contact','$uploadedImgFileName')";
    echo "Image Uploaded Successfully";
echo "<br/><b>Name:</b> " . $uploadedImgFileName . "<br>";
echo "<b>Type:</b> " . $uploadedImgFileExtension . "<br>";
echo "<b>Size:</b> " . ($uploadedImgFileSize / 1024) . " kB<br>";
} else {
     echo "ERROR: File not moved correctly";
echo "Invalid Image File";

Written by

We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes. Each support has free and providing HD support screen casting.


Post a Comment

PHP Training Mumbai, PHP MySQL Training Mumbai, PHP Course Mumbai, PHP Institute Mumbai


© 2013 PHP TRAINING IN MUMBAI BY OM SIR . All rights resevered. Designed by OM SIR

Back To Top