hi guys i need your help im not very good with ajax im trying to upload a image using a form with php the image is not getting to the php i got error on
Notice: Undefined index: ImageFile in line 9
the line is this one
move_uploaded_file($_FILES['ImageFile']['tmp_name'], "$Destination/$NewImageName");
this is my jquery
$("#send_friend").click(function(e) {
e.preventDefault();
$("#message").empty();
$('#loading').show();
// getting the values that user typed
var add_friends = '1';
var friend_name = $("#friend_name").val();
var friend_url = $("#friend_url").val();
var ImageFile = $('#ImageFile').prop('files')[0];
// forming the queryString
var data=
'add_friends=' + add_friends +
'&clan_name=' + clan_name +
'&friend_name=' + friend_name +
'&friend_url=' + friend_url +
'&ImageFile=' + ImageFile;
// ajax call
$.ajax({
type: "POST",
url: "components/friends.php",
data: data,
success: function(data){ // this happen after we get result
$('#loading').hide();
$("#message").html(data);
}
});
return false;
});
this is my php
if(isset($_POST['add_friends'])){
include "../_database/database.php";
$Destination = '../imagenes/friends';
$friend_name = $database ->real_escape_string(htmlentities($_REQUEST['friend_name']));
$friend_url = $database ->real_escape_string(htmlentities($_REQUEST['friend_url']));
if(!isset($_FILES["ImageFile"]) || !is_uploaded_file($_FILES["ImageFile"]["tmp_name"])){
$NewImageName= 'default.png';
move_uploaded_file($_FILES["ImageFile"]["tmp_name"], "$Destination/$NewImageName");
}
else{
$RandomNum = rand(0, 999);
$ImageName = str_replace(' ','-',strtolower($_FILES['ImageFile']['name']));
$ImageType = $_FILES["ImageFile"]["type"];
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
move_uploaded_file($_FILES["ImageFile"]["tmp_name"], "$Destination/$NewImageName");
}
$sql = "INSERT INTO family (name,url, shield)
VALUES ('".$friend_name."', '".$friend_url."', '".$NewImageName."')";
$r1 = mysqli_query($database,$sql);
$sqlResult = $r1;
/*** run the sql statement ***/
}
and my html
<div class="form-group">
<label for="exampleInputFile" class="supercell">Shield</label>
<input type="file" id="ImageFile" name="ImageFile"/>
only with the file i have some problems the database is been populated with all the other vars but can´t get the file to pass to the php any help will be really appreciated
You need to set the contentType to multipart/form-data because the form includes a file.
type: "post",
contentType: "multipart/form-data"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.