Kavya Shree Kavya Shree - 1 year ago 94
jQuery Question

Upload image in database using PHP and AJAX

Hiii Everyone,

I tried to upload image and i want to store image path in database using php and ajax.Here is my code.I tried with this but im getting error. i searched everywhere but i can't find the solution.

HTML page:

<form id="reg-form">
<input name="image" type='file' class="inputFile" id="photo" ACCEPT="image/*"/>
<a href='#' class='ph-button ph-btn-blue' id="register_translator">Register</a>

And my Javascript code

$('#register_translator').click(function (event)
url: "add_translator_image.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
success: function(data){
error: function(){}


PHP code

$date = date("Y-m-d");
$time = date("Y-m-d H:i:s");
require "translator.php";

foreach ($_FILES['image']['name'] as $name => $value)
$sourcePath = $_FILES['image']['tmp_name'][$name];
$targetPath = "uploads/".$_FILES['image']['name'][$name];
$reg = new translator();

$add = $reg->add_translator_image($targetPath);

echo $add;


And in add_translator_image i just wrote insert query in database.Error is
Undefined index: image.. Image is name given for input type file it is not taking value so simply wherever im using image it is showin undefined error.What is the mistake.I tried many times but i cant find the solution.If anybody know the solution please let me know as soon as possible.If you help me with this part i ll be grateful.Thanks in advance.

Answer Source

new FormData must be call existed form id with index 0 [javascript standard object]..

data: new FormData($('#reg-form')[0]),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download