0

i am creating the simple crud system using php ajax.in the crud system i add the image also.but when i fill the data and browse image and click add button record is not added in to the database.what i tried so far i attached below.no error shown on the console.i think might be the problem with jquery send the form values and images data: {form_data: form_data,data: data},

Form Design

 <form role="form" id="frmcompany" class="card" enctype="multipart/form-data">
        <div align="left">
            <h3>Company</h3>
        </div>

        <div align="left">
            <label class="form-label">Company name</label>
            <input type="text" class="form-control" placeholder="Patient No" id="cno" name="cno" size="30px" required>
        </div>

        <div align="left">
            <label class="form-label">Country</label>
            <input type="text" class="form-control" placeholder="Patient Name" id="coutry" name="coutry" size="30px" required>
        </div>

        <div align="left">
            <label class="form-label">Currency</label>
            <input type="text" class="form-control" placeholder="Phone" id="currency" name="currency" size="30px" required>
        </div>

        <div align="left">
            <label class="form-label">Address</label>
            <input type="text" class="form-control" placeholder="Address" id="address" name="address" size="30px" required>
        </div>
            <div align="left">
                <div class="fileuploader fileuploader-theme-default">
                    <input type="hidden" name="fileuploader-list-files_" value="[]">
                    <input type="file" id="file" name="file" >
                    <div class="fileuploader-items">
                        <ul class="fileuploader-items-list"></ul>
                    </div>

                </div>
            </div>

        </br>

        <div align="right">
            <button type="button" id="save" class="btn btn-info" onclick="addPatient()">Add</button>
            <button type="button" id="clear" class="btn btn-warning" onclick="reset()">Reset</button>

        </div>


    </form>

jquery

            function addPatient()
{
    var upload_date = $('#file').prop('files')[0];
    var form_data = new FormData();
    form_data.append('file', upload_date);


    if($("#frmcompany").valid())
    {
        var url = '';
        var data = '';
        var method = '';

        if(isNew == true)
        {
            url = 'php/add_patient.php';
            data = $('#frmcompany').serialize() ;
            method = 'POST';
        }


        $.ajax({
                type : method,
                url : url,
                dataType : 'JSON',
            cache: false,
            contentType: false,
            processData: false,
               // data:  data,

            data: {form_data: form_data,data: data},

            success:function(data)
            {




                    if (isNew == true) {
                        alert("Company Addedd");
                    }



            }

        });

    }

}
$('#file').fileuploader
({
    limit: 1,
});

            <div align="right">
                <button type="button" id="save" class="btn btn-info" onclick="addPatient()">Add</button>
                <button type="button" id="clear" class="btn btn-warning" onclick="reset()">Reset</button>

            </div>

        </form>

php code

<?php

$servername = "***";
$username = "***";
$password = "***";
$dbname = "***";
$conn = new mysqli($servername,$username,$password,$dbname);

if($conn->connect_error)
{
    die("connection failed" . $conn->connect_error);
}

if($_SERVER['REQUEST_METHOD'] == 'POST')
{

   if (0 < $_FILES['file']['error'])
    {
        echo 'Error: ' . $_FILES['file']['error'] . '<br>';
    }
    else
   {
        move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
   }

    $stmt = $conn->prepare("insert into company(companyname,coutry,currency,address,image) VALUES (?,?,?,?,?)");
    $stmt->bind_param("ssss",$companyname,$coutry,$currency,$address,$image);
    $companyname = $_POST['cno'];
    $coutry = $_POST['coutry'];
    $currency =  $_POST['currency'];
    $address =  $_POST['address'];
    $image = $_FILES['file']['name'];

    if($stmt->execute())
    {
        echo 1;
    }
    else
    {
        echo 0;
    }
$stmt->close();
}
?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

2 Answers2

0
 function addPatient()
{
    var upload_date = $('#file').prop('files')[0];
    var form_data = new FormData();
    form_data.append('file', upload_date);

      $.ajax({
            url: 'php/add_patient.php',
            cache: false,
            contentType: false,
            processData: false,
            data: form_data,
            type: 'POST',
            success: function(response) {
                console.log(response);
            },
            error: function(error) {
                console.log(error);
            }
        });
}
Mohit Kumar
  • 952
  • 2
  • 7
  • 18
0

You can't send a FormData object inside another object. All the POST parameters have to be in form_data.

function addPatient() {
  if ($("#frmcompany").valid()) {
    var url = '';
    var data = '';
    var method = '';
    var form_data = new FormData(document.getElementById("frmcompany"));
    var upload_date = $('#file').prop('files')[0];
    form_data.append('file', upload_date);

    if (isNew == true) {
      url = 'php/add_patient.php';
      data = $('#frmcompany').serialize();
      method = 'POST';
    }

    $.ajax({
      type: method,
      url: url,
      dataType: 'JSON',
      cache: false,
      contentType: false,
      processData: false,
      data: form_data,
      success: function(data) {
        if (isNew == true) {
          alert("Company Addedd");
        }
      }
    });
  }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612