1

I am building a form to process text input, multiple check boxes and 4 images. currently I am to process the check boxes using the each function to put all the values of the checkboxes in an array before sending it through ajax. Now the problem is that I can't send the images with ajax too. And also I can't access the images too.

Code:

 $(document).ready(function () {
    //alert("this page works");
    $('#uploadProperty').on('submit',function  (e){
        e.preventDefault();

        var hname    = $('#hname').val();
        var location = $('#location').val();
        var htype    = $('#htype').val();
        var rooms    = $('#rooms').val();
        var price    = $('#price').val();
        var hdetails = $('#hdetails').val();
        var feature  = [];
        $('.feature').each(function() {
            if($(this).is(":checked")){
                feature.push($(this).val());
            }
        });
        // if (feature.length == 0)
        //  alert("Select atleast 1 Feature");
        // else{
        //  feature = feature.toString();
        //  alert(feature);
        // }
        var file1 = $('#file4').val();
        //alert(file1);
        $.ajax({
            url     : 'core/upload.php',
            type    : 'POST',
            data    : new FormData(),
            contentType : false,
            processData : false,
            success : function (ep){
                alert(ep);
            }
        });

    });
});
Marco Santarossa
  • 4,058
  • 1
  • 29
  • 49
Dapo Michaels
  • 380
  • 2
  • 4
  • 16

2 Answers2

1

You need to upload images first via ajax ( ex: http://hayageek.com/docs/jquery-upload-file.php ) and after make another ajax for the form fields. But you need an ID link between Property and images. you cand add an empty record and remember the mysql_insert_id to make update with the form fields and insert images or update ( depend how is your table structure )

CatalinB
  • 571
  • 4
  • 11
0

So if i got it right, you want to fill the FormData object. Because currently it's empty. You can use append method:

    var formData = new FormData();
    var $myField = $('#myField');
    formData.append('myField', $myField.val());

To append file:

    var $fileField = $('#fileField');
    var myFile = $fileField.get(0).files[0];
    formData.append('myFile', myFile);

To append multiplie files you should set the name properly:

    formData.append('myFile[]', myFileFirst);
    formData.append('myFile[]', myFileSecond);

Check it here: Uploading multiple files using formData()

Also, you can grab the whole form data through constructor:

    var form = $('form').get(0);
    var formData = new FormData(form);
Alexey Chuhrov
  • 1,787
  • 12
  • 25
  • thanks for this answer but if i go with any of your suggestions how do i get each unique field in php so i can insert them into specific fields in my database table? – Dapo Michaels Sep 06 '16 at 07:41
  • The same as usual. Using $_POST and $_FILES superglobals. Here's in the end Example #3, how to handle multiplie file uploading using foreach http://php.net/manual/en/features.file-upload.post-method.php – Alexey Chuhrov Sep 06 '16 at 08:34
  • its finally working... thanks guys.. i can now send all the data i need through appending method. – Dapo Michaels Sep 06 '16 at 16:09