0

I have a jQuerySteps form setup where a user can upload a CSV file to eventually populate different database tables. However I have a lot of validation steps.

When a user gets to the end step, and they click 'Complete Upload' I am running an AJAX request onClick to Serialize the form data then pass this through to a PHP file which will A) process the file and B) log the file upload.

Im currently having difficulties with passing the relevant array data to the PHP file via $_POST, I have spent 4 hours scouring the web but I cannot find a solution.

My function which runs when the final 'Complete Upload' Complete Upload:

        onFinished: function (event, currentIndex)
    {

            // Create array of form input values
            var postData = $("#signup-form").serializeArray();

            //Find the input file selected by the user
            var fileVal = document.getElementById("fileToUpload").value;

            //Update postData array with file name
            postData.push({name: 'fileupload', value: fileVal});
            postDataStringify = JSON.stringify(postData);

            console.log(postDataStringify);

            $.ajax({
                   method : 'POST',
                   url : '/ecommerce/store_configuration/master-form-process-data.php',                       
                   data: {newData: postDataStringify},
                   processData: false,  // tell jQuery not to process the data
                   contentType: false,  // tell jQuery not to set contentType
                   success : function(data) {

                        console.log(data); // for debugging

                   },
                   error: function() {
                       alert("There was an error"); 

                  }
            });      
    }

My PHP file:

session_start();

// Posting without json decode as stringify was completed
$getArray = $_POST['newData'];

// Access array data
$dtype= $getArray[0]["datatype"]; 

// Console log the value if present
echo json_encode($dtype);

Im getting the correct stringify array being logged in the console:

[{"name":"datatype","value":"branddata"},{"name":"fileDesc","value":"sadasd"},{"name":"ref","value":"asddsa"},{"name":"user","value":"admintest1"},{"name":"fileupload","value":"C:\\fakepath\\brand_export (4).csv"}]

However im just not seeing any $_POST data coming through via var_dump or anything, with the following error:

<b>Notice</b>:  Undefined index: newData in <b>/home/oqck4tw7fpkt/public_html/ecommerce/store_configuration/master-form-process-data.php</b> on line <b>5</b><br />

Can anyone offer me any help with this?

Stan Howe
  • 116
  • 2
  • 11
  • You need to use `FormData` to upload files using ajax. Something like `formData = new FormData($('#signup-form'));` and then `data: formData` in the .ajax call – Nick Jun 02 '20 at 02:21
  • You are not passing formData and passing json data so remove processData and contentType from ajax call, it will work – Tufail Ahmad Jun 02 '20 at 08:09

0 Answers0