Upload Multiple File in Php using Javascript


Result.php

 <form>
                <div class="form-group Row">
                    <label for="ReportResult" class="col-sm-2 form-control-label">Report Result File:</label>
                    <div class="col-sm-6">
                        <input type="file" class="form-control-file" name="reportfile[]" id="reportfile"  accept="image/*,application/pdf" multiple>
                    </div>
                </div>
                <div class="form-group Row">
                    <label for="remark" class="col-sm-2 form-control-label">Remark:</label>
                    <div class="col-sm-6">

                        <textarea class="form-control" id="remark"></textarea>
                    </div>
                </div>

                <div class="form-group Row">
                    <input type="button" value="Submit" id ="btnreport" onclick="Upload_Report()" class="btn  btn btn-primary">

                    <input type="reset" value="Cancel" class="btn  btn btn-primary">

                </div>
            </form>

Report.js

Get Data From Data Base

function Result() {
    report_no = $("#ResultReportid").val();
    if (report_no == '' || report_no == null) {
        alert('Please Enter Report no');

    } else {
        $.ajax({
            url: getreportresulturl,
            type: "POST",
            data: {
                'report_no': report_no
            },
            dataType: "json",
            async: false,
            success: function (data) {

                console.log(data);
                $('#Reportresult').empty();
                $('#Gallery').empty();
                $('#Pdf').empty();
                if (data.success == 1) {

                    $('#result_name').val(data.data[0].first_name + " " + data.data[0].middle_name + " " + data.data[0].last_name);
                    $('#result_contactno').val(data.data[0].mobile_no);
                    $('#result_age').val(data.data[0].age);
                    $('#result_date').val(data.data[0].result_date);
                    $('#result_reportname').val(data.data[0].report_name);

                    localStorage.setItem("report_info_id", data.data[0].report_info_id);
                    localStorage.setItem("report_id", data.data[0].report_id);
                    localStorage.setItem("result_flag", data.data[0].result_flag)

                    if (data.data[0].result_flag == 1) {
                        var image = new Array();
                        var pdf = new Array();
                        localStorage.setItem("report_result_id", data.data[0].result_data[0].report_result_id)

                        if (data.data[0].result_data[0].result_image != "NA") {
                            image = data.data[0].result_data[0].result_image.split(',');
                            //console.log(image);
                            var text = "";

                            for (var i = 0; i < image.length; i++) {
                                var url = imageURL + image[i];
                                text = text + "<div class=\"col-md-2\">      <img src=" + url + " alt=" + image[i] + " class=\"thumbnail\" style=\"height:200px;width:200px\"></div>"
                            }
                            $('#Gallery').append(text);
                        }
                        if (data.data[0].result_data[0].result_pdf != "NA") {
                            pdf = data.data[0].result_data[0].result_pdf.split(',');
                            //console.log(image);
                            var pdftext = "";

                            for (var i = 0; i < pdf.length; i++) {
                                var url = imageURL + pdf[i];
                                pdftext = pdftext + "<div class=\"col-md-2\"><a   class=\"icon-bar\" href=" + url + " target=\"blank\" alt=" + pdf[i] + " >" + pdf[i] + "</a></div>"
                            }
                            $('#Pdf').append(pdftext);
                        }
                    }
                } else {
                    var text = "<p style=\"color:red\">No Record Avilable for '" + report_no + "' </p>";
                    $('#Reportresult').append(text);
                    $('#result_name').val(' ');
                    $('#result_contactno').val(' ');
                    $('#result_age').val(' ');
                    $('#result_date').val(' ');
                    $('#result_reportname').val(' ');
                }
            }

        });
    }
}

Upload Image

function Upload_Report() {
    var remark = $('#remark').val();
    var lab_staff_id = localStorage.getItem("Lab_staff_id");
    var report_id = localStorage.getItem("report_id");
    var report_info_id = localStorage.getItem("report_info_id");
    var result_flag = localStorage.getItem("result_flag");
    var report_result_id;
    if (result_flag == 0) {
        report_result_id = 0;
    } else {
        report_result_id = localStorage.getItem("report_result_id");
    }
    var fd = new FormData();
    var myfile = document.getElementById('reportfile').files
    if (myfile.length != 0) {
        for (var i = 0; i < myfile.length; i++) {
            var reader = new FileReader();
            reader.onloadend = function (e) {

            };
            reader.readAsDataURL(myfile[i]);
            if (fd) {
                fd.append("Report[]", myfile[i]);
            }
        }
  

        fd.append("report_id", report_id);
        fd.append("report_info_id", report_info_id);
        fd.append("report_tester_id", lab_staff_id);
        fd.append("lab_staff_id", lab_staff_id);
        fd.append("description", remark);
        fd.append("result_id", report_result_id);
        $.ajax({
            url: InsertReportfileurl,
            type: "POST",
            data: fd,
            dataType: "json",
            async: false,
            processData: false,
            contentType: false,
            
            success: function (data, textStatus, jqXHR) {
                console.log(data);
                if (data.success == 1) {
                    $('#Gallery').empty();
                    $('#Pdf').empty();
                    $('#result_name').val(' ');
                    $('#result_contactno').val(' ');
                    $('#result_age').val(' ');
                    $('#result_date').val(' ');
                    $('#result_reportname').val(' ');
                    $('#remark').val(' ');
                    $('#reportfile').val(' ');
                    $.notify("Record insert Successfully", "success");
                 } 
             else {
                    $.notify("Record not inserted", "error");

                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(errorThrown);
                $.notify("Error in insert data", "error");
            }
        });
    } else {
        $.notify("Please Select File", "error");
    }
}


PHP File

Insert_image.php


<?php
header("Content-Type:application/json");
include("laboratory_function.php");

if(isset($_POST['report_id']) && isset($_POST['report_info_id']) && isset($_POST['report_tester_id']) && isset($_POST['lab_staff_id'])
  && isset($_FILES['Report']) && isset($_POST['description']))
{
$data = insert_dev_lab_report_result($_POST['report_id'],$_POST['report_info_id'],$_POST['report_tester_id'],$_POST['lab_staff_id'],$_FILES['Report'], $_POST['description']);

$json_response = json_encode($data);
echo $json_response;
}
else
{
echo "invalid Request";
}
?>

laboratory_function.php

function insert_dev_lab_report_result($report_id,$report_info_id,$report_tester_id,$lab_staff_id,$Report,$description)
{
include 'connect.php';

     $respone = insert_File($Report);
   
    $imgvalue = 'NA';
    $pdfvalue = 'NA';
    if(count($respone)>0)
    {
    if($respone['image'] != ' ')
    {
    $imgvalue = $respone['image'];
    }
    if($respone['pdf'] != ' ')
    {
    $pdfvalue = $respone['pdf'];
    }
    }
    

    $sql = "INSERT INTO dev_lab_report_result (report_id,report_info_id,report_tester_id,lab_staff_id,result_image,result_pdf,description,date_time) values ('$report_id','$report_info_id','$report_tester_id','$lab_staff_id','$imgvalue','$pdfvalue','$description',now())";

$result = mysqli_query($connection,$sql2);

if ($result)
{


$sql1 = "UPDATE dev_lab_report_info set status = '6' where report_info_id = '$report_info_id'";
    $result1 = mysqli_query($connection,$sql2);

    if($result1)
    {
    $sql3 = "SELECT count(status) as pp from dev_lab_report_info where report_id = '$report_id' and status != '6'";
    $result3 = mysqli_query($connection,$sql2);
   

    $row = mysqli_fetch_assoc($result3);



    if($row['pp'] != 0)
    {
    return   array('success' => 1 ,'message' => 'not update successfully');
    }
    else
    {
   
$sql2 = "UPDATE dev_doc_report_prescription set status = '6' where report_id = '$report_id'";
$result2 = mysqli_query($connection,$sql2);

if($result2)
{
return   array('success' => 1 ,'message' => 'update successfully');
}

    }
}
}
else
{
return   array('success' => 0 ,'message' => 'insert fail');
}
   
    }
function insert_File($file_name)
{

$IMG = count($file_name)>=0? $file_name : array();
if (!empty($IMG)) {
    $uploads_dir = 'challengeProofMedia/';
$value_insert1=array();
$value_insert2=array();
    foreach ($IMG["error"] as $key => $error)
     {

        if ($error == UPLOAD_ERR_OK)
        {
           $tmp_name = $IMG["tmp_name"][$key];
           $name = $IMG["name"][$key];
   
           
     if($IMG['type'][$key] == 'image/jpeg' or $IMG['type'][$key]  == 'image/jpg'  or $IMG['type'][$key]  == 'image/png')
    {
     $currentdate = date("Ymdhis");
$temp = explode(".",$name);
$newfilename = $currentdate ."_profile_".$key.'.' .end($temp);
     move_uploaded_file($tmp_name, "$uploads_dir/$newfilename");
    $name_array=mysql_real_escape_string($newfilename);
     $value_insert1[] = $name_array;
    }
        else if ($IMG['type'][$key] == 'application/pdf')         {
         $currentdate = date("Ymdhis");
$temp = explode(".",$name);
$newfilename = $currentdate ."_profile_".$key.'.' .end($temp);
       move_uploaded_file($tmp_name, "$uploads_dir/$newfilename");
       $name_array=mysql_real_escape_string($newfilename);
       $value_insert2[] = $name_array;
       
        }
         
      }
    }
    $values_insertimg = implode(',', $value_insert1);
    $values_insertpdf = implode(',', $value_insert2);

    return  array('image' =>$values_insertimg ,'pdf'=>$values_insertpdf);
 
}
else
{ return array();
}

}



Comments

Popular posts from this blog

Periodic Restart Settings for Application Pool Recycling

Generate image ,pdf and Video Thumb using node js

How to receive JSON object in POST and GET using web api