Pass image and data to php via AJAX

ساخت وبلاگ
Hii fellow coders, Sihle here from South Africa, i am developing a website and i am trying to pass data to php via ajax and it works when its just input data from text fields however when i add an image as input in my html and store this in a javascript variable to send to image_upload.php, i get a "Notice: Undefined index: images in C:wamp64wwwNG_Berkshire_CPFbreakeven-832584588image_upload.php on line 6". Please see below code that i have.

identity_database.php
Code:

<script type="text/javascript">
       
$(document).ready(function(){
$("#fsubmit").click(function(){
var img = $('input[type="file"]').val(); //.files[0] i have tried this code too
var imgdetail = $("#detail").val();
var verify = "fsubmit";
//formdata = new FormData();
//formdata.append('images', document.getElementById("images").val());
//formdata.append('detail', imgdetail);
//formdata.append('fsubmit', verify);

if(img == ""){
    $("#message").html("<span style='color:red;'>The image field should not be left empty.</span>");
}
else
{

$.ajax({
    type: "POST",
url: "image_upload.php",
data:"images="+img+"&detail="+imgdetail+"&fsubmit="+fsubmit,
//contentType: false,
//processData: false,
success: function(data){
 //alert(data); 
$("#message").html(data);
}
});
}
return false;
});
});
    </script>

<form method="post" id="imgform" name="imgform" action="" enctype="multipart/form-data">
<input class="btn btn-secondary" type="file" name="images"  id="images"  onchange="loadFile(event)">
<textarea class="input-texts" id="detail" name="detail" placeholder="Details"></textarea>

<div class="detailss">
<button class="btn btn-secondary" type="submit" name="fsubmit" id="fsubmit">Upload</button>
</form>

then below is part of my php script

Code:

<?php
include_once('cpfdb-config.php');

if(isset($conn, $_POST['fsubmit'])){
$target_dir = "http://www.codingforums.com/images/uploads/";
$image = $_FILES['images']['name'];
$details = mysqli_real_escape_string($conn,$_POST['detail']);
$dateadded = date("Y-m-d H:i:s");

$validextensions = array("jpeg", "JPG", "png", "gif", "jpg", "JPEG", "PNG", "GIF");      // Extensions which are allowed.
$ext = explode('.', basename($_FILES['images']['name']));  // Explode file name from dot(.)
$file_extension = end($ext);

$target_path2 = $target_dir.md5(uniqid()). "." .$ext[count($ext) -1];// Set the target path with a new name of image.
$image = $target_path2;
    // Increment the number of uploaded images according to the files in array.
if (($_FILES["images"]["size"] < 1000000)    // Approx. 100kb files can be uploaded.
&& in_array($file_extension, $validextensions)) {
        if (move_uploaded_file($_FILES['images']['tmp_name'], $target_path2)) {\some code here}
?>

CodingForums...
ما را در سایت CodingForums دنبال می کنید

برچسب : نویسنده : codingforums بازدید : 163 تاريخ : چهارشنبه 28 تير 1396 ساعت: 12:53