Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Upload An Image Without A Form Submitting Using Ajax

Upload an image without a form submitting using ajax

Posted on August 30, 2019April 10, 2020 By XpertPhp No Comments on Upload an image without a form submitting using ajax

Today, In this tutorial, we will be explaining how to Upload an image without a form submitting using ajax. many people say it is possible to upload an image without a form submitting but it is possible. so let’s go we use this tutorial to make possible.

We have used jquery and ajax and in this below file. we have taken also FormData() object.

When the user clicks on button then we will get the image data and it is passed into FormData() object and call the ajax with FormData() object data.

index.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Upload an image without a form submitting using ajax - XpertPhp</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
  <script>
$(document).ready(function(){
$('#btnUpload').on('click', function() {
var file_data = $('#fileImage').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
 
$.ajax({
url         : 'upload.php',    
dataType    : 'text',          
cache       : false,
contentType : false,
processData : false,
data        : form_data,                        
type        : 'post',
success     : function(output){
if(output=="File Uploaded Successfully")  
{
$('.error').html('<div class="alert alert-success"><strong>Success!</strong> '+output+'</div>');
}
else if(output=="Only allowed jpeg,jpg and png extension")
{
$('.error').html('<div class="alert alert-danger"><strong>Danger!</strong> '+output+'</div>');
}
else{
$('.error').html('<div class="alert alert-danger"><strong>Danger!</strong> '+output+'</div>');
}
}
});                
});
});
</script>
</head>
<body>
 
<div class="container" style="margin-top:25px;">
  <div class="error"></div>
    <div class="form-group">
<label for="fileImage">Upload a Image:</label>
<input id="fileImage" type="file" name="fileImage" />
    </div>
<button id="btnUpload" class="btn btn-default">Upload</button>
</div>
</body>
</html>

upload.php

When ajax call the upload.php file then the image will be upload into images directory.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
$src = $_FILES['file']['tmp_name'];
$targ = "images/".$_FILES['file']['name'];
 
$validextensions = array("jpeg", "jpg", "png");
$temporary = explode(".", $_FILES["file"]["name"]);
$file_extension = end($temporary);
if(in_array($file_extension, $validextensions))
{
if(move_uploaded_file($src, $targ))
{
echo "File Uploaded Successfully";
}
else
{
echo "Failed";
}
}
else{
echo "Only allowed jpeg,jpg and png extension";
}
 
?>

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • Increase or decrease a value for inputs using jquery
  • allow only numbers in textbox jquery on keypress
  • upload a file with JavaScript and PHP
  • Codeigniter CRUD Operation With Ajax Example
  • Convert XML into Associative Array in PHP
Ajax, Jquery, Php Tags:Ajax File Upload PHP, file upload, Image Upload, jQuery Ajax Form Handling, jquery tutorial, PHP File Upload Script, php with mysql

Post navigation

Previous Post: preview and upload image using php with mysql
Next Post: Drag and Drop rows using jquery with ajax example

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com