I have the following files. Nothing is displayed in the Response header of developer tools. I am expecting the variables related to files and post in my accept.php
page.
I can see console.log(formData);
getting printed but console.log(returndata);
1) send.php
file
<?php
echo '
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type="text/javascript" src="file.js"></script>
<form id="data" enctype="multipart/form-data">
<input type="hidden" name="id" value="123" readonly="readonly">
User Name: <input type="text" name="username" value=""><br />
Profile Image: <input name="profileImg[]" type="file" /><br />
Display Image: <input name="displayImg[]" type="file" /><br />
<button type = "button" onclick="submitFileTest()">Submit Button </button>
</form>';
?>
2) file.js
function submitFileTest() {
//grab all form data
var formData = new FormData($(this)[0]);
console.log("Form Data Test");
console.log(formData);
$.ajax({
url: 'accept.php',
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returndata) {
//alert(returndata);
console.log(returndata);
}
});
}
3) accept.php
<?php
if (isset($_POST['id'])) {
$id = $_POST['id'];
var_dump($id);
echo $id;
}
if (isset($_POST['username'])) {
$username = $_POST['username'];
var_dump($username);
echo $username;
}
if (isset($_FILES['profileImg'])) {
$profileImg = $_FILES['profileImg'];
var_dump($profileImg);
echo $profileImg;
}
if (isset($_FILES['displayImg'])) {
$displayImg = $_FILES['displayImg'];
var_dump($displaying);
echo $displayImg;
}
?>
TESTING RESULTS (after using Popmedic answer):
Before testing, for displayImg
,I changed the file array value in the following line formData.append('displayImg', $('input[type=file]')[1].files[0]);
from 0
to 1
as shown below formData.append('displayImg', $('input[type=file]')[1].files[1]);
I am able to see the id
value, username
value and profileImg
value in my php script as shown in the screenshot of console window of IE11 below. But for some reason, the code is not picking up the second file which is displayImg
.
The following is the screenshot of Request body
of IE11 showing that for the file displayImg
, there is nothing present.
Clarifications on Popmedic's comments (April 18):
I have merged the POST and FILEs results using this solution and got the following in the JSON format:
{"id":"123","username":"123","profileImg":{"name":"Request_body.PNG","type":"image\/png","tmp_name":"C:\\wamp\\tmp\\phpA981.tmp","error":0,"size":15842},"displayImg":{"name":"Request_body.PNG","type":"image\/png","tmp_name":"C:\\wamp\\tmp\\phpA9A1.tmp","error":0,"size":15842}}
I already have a Java webservice working which is accepting a JSON object and storing it in the Oracle database in the form of CLOB. So that's the reason I merged and converted into JSON as shown above. I would like to include the variable $str
value also in the JSON response above obtained from this line of code$str = file_get_contents($_FILES["displayImg"]["tmp_name"]);
so that in addition to the above JSON, I could also include the file contents to the webservice and that Java webservice will make sure that it's inserted into the Oracle database. eventually my cuRL request would look like this:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_PORT => "9090",
CURLOPT_URL => "http://localhost:9090/myJavaWebservice/rest/somepath/insertDataToDB",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $_POST["myData"],
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response_post_march22 = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response_post_march22;
}
I already have the above curl request working and inserting the data into the database but now instead of myData
which is a JSON object
, I would be passing the new JSON, I created above after merging including the file content obtained using the $str
variable so that everything can be stored into the database.