In HTML I have:
<input name="feature[1]" value="1" type="hidden">
<input name="feature[2]" value="1" type="hidden">
<input name="feature[3]" value="1" type="hidden">
And in Javascript I have:
features = $.map($('#add-place [name^="feature"]'), function(item, index)
{
if($(item).val()=='1')
{
return $(item).attr('name').replace(/[^0-9]/g, '');
}
});
When I console.log(features) it returns an array but when I send it through ajax with new FormData in PHP I will have an string containing the numbers in the features array so if they are [1, 3] I will get 1,3 as string in PHP.
What causes this?
EDIT
form = new FormData();
features = $.map($('[name^="feature"]'), function(item, index){if($(item).val()=='1'){return $(item).attr('name').replace(/[^0-9]/g, '');}});
form.append('features', features);
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
enctype: 'multipart/form-data',
cache: false,
processData: false,
contentType: false,
data: form,
complete: function(response)
{
//
}
});
Try to set processData to false on ajax query:
$.ajax({
url: '',
data: formData,
processData: false
});
Related
In my js class, I am sending form data via ajax to the php that is fine but I have an array which doesn't belong to that form how can I send it with form data
$("#formed").unbind('submit').bind('submit', function() {
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data:new FormData(this),
dataType: 'json',
cache: false,
contentType: false,
processData: false,
async: false,
success: function(response) { }
});
});
Now there is array
var myList = new Array();
It doesn't belong to form but I want to send it making a json array with above ajax call?
ANY SOLUTION!!!!!
you can use this function formData.append(name, value);
$("#formed").unbind('submit').bind('submit', function() {
var data = new FormData(this);
var myList = [];
// add your data here
// to send an array as data you should convert it to the string
data.append('testField', JSON.stringify(myList));
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data:data,
dataType: 'json',
cache: false,
contentType: false,
processData: false,
async: false,
success: function(response) { }
});
});
$(document).on('submit', '#color_changer_form', function(e) {
e.preventDefault();
var colorCode = $('#color_code').val();
var formData = new FormData(this)[0];
$.ajax({
headers: {
'X-CSRF-Token': "{{csrf_token()}}"
},
type: "POST",
url: "{{route('color.store')}}",
data: formData,
async: false,
success: function(data) {
console.log(data)
},
cache: false,
contentType: false,
processData: false
});
});
<form action="" method="POST" id="color_changer_form">
<input type="text" id="color_code" name="color_code">
<button type="submit" id="color_submit" class="btn btn-success">Save Change</button>
</form>
Controller snippet:
public function store(Request $request){
return response()->json($request->all());
}
When I try to get the whole form data using the jQuery AJAX FormData() method, I get an empty array.
In need to use this FormData() because in the near future I have to upload an image using the form data.
Send the whole formData object
Change:
var formData = new FormData(this)[0];
To
var formData = new FormData(this);
If there are no files involved it is simple to use serialize() also
$.ajax({
headers: {
'X-CSRF-Token': "{{csrf_token()}}"
},
type: "POST",
url: "{{route('color.store')}}",
data: $(this).serialize(),
success: function(data) {
console.log(data)
}
});
Never use async:false. it is a terrible practice and is deprecated
I'd like to pass a PHP session variable (called 'profileid') using FormData and AJAX. I thought this below would work, it did not. Why?
var imageData = new FormData();
imageData.append('image', $('#uploadImage')[0].files[0]);
imageData.append('profileid', <?php echo $_SESSION['profileid'];?>);
//Make ajax call here:
$.ajax({
url: '/upload-image-results-ajax.php',
type: 'POST',
processData: false, // important
contentType: false, // important
data: imageData,
//leaving out the rest as it doesn't pertain
You could add the profileid in the $.ajax URL parameter instead of adding it in FormData:
$(document).ready(function (e) {
$('#uploadImageForm').on('submit',(function(e) {
e.preventDefault();
var formData = new FormData(this);
$.ajax({
url: "/upload-image-results-ajax.php?profileid=<?= $_SESSION['profileid']; ?>",
type: "POST",
data: formData,
cache: false,
contentType: false,
processData: false,
success: function(response){
console.log("success");
console.log(response);
},
error: function(response){
console.log("error");
console.log(response);
}
});
}));
$('#uploadImage').on("change", function() {
$("#uploadImageForm").submit();
});
});
Don't forget to place session_start(); at the beginning of your code.
I am trying to add images, few string information and array using formData. I can successfully add images, string but can't add array information while adding. It always passes array as a string.Any help would be appreciated. My code
var reader = new FileReader();
var form_data = new FormData();
form_data.append('departure_city',departure_city);
form_data.append('inclusion_icons', inc_icons);
form_data.append('theme_icons',theme_icons);
$.ajax({
url: url,
type: "POST",
async: false,
cache: false,
contentType: false,
processData: false,
enctype: 'multipart/form-data',
data: form_data,
}).done(function (data) {
console.log("Success");
console.log(data);
}).fail(function (data) {
console.log("Error");
console.log(data);
});
inclusion_icons and theme_icons are arrayz, however I'm getting them as a string.
Currently using this:
function acceptimage() {
var data = new FormData();
jQuery.each($('#uploadImage')[0].files, function(i, file) {
data.append('uploadImage-'+i, file);
});
$.ajax({
url: 'upload.php',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
popup('popUpDiv');
ias.cancelSelection();
ias.update();
}
});
};
And it's sending my file perfectly, but I need to send 4 field values along with it. Could anyone let me know how I post:
<input type="hidden" id="x" name="x" />
<input type="hidden" id="y" name="y" />
<input type="hidden" id="w" name="w" />
<input type="hidden" id="h" name="h" />
Along with the file? Many thanks
function acceptimage() {
var data = new FormData();
jQuery.each($('#uploadImage')[0].files, function(i, file) {
data.append('uploadImage-'+i, file);
data.append('x', $("#x").val());
data.append('y', $("#y").val());
data.append('w', $("#w").val());
data.append('h', $("#h").val());
});
$.ajax({
url: 'upload.php',
data: data,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
popup('popUpDiv');
ias.cancelSelection();
ias.update();
}
});
};