can't get any file data, It shows file data empty [duplicate] - javascript

This question already has answers here:
jQuery AJAX file upload PHP
(5 answers)
Closed 5 years ago.
I did this for uploading files via ajax.
But I can't get any file data. It shows file data empty.
Here is my ajax part.
$("#personal_image").on('click',function(event) {
event.preventDefault();
var datastring = $("#personal_image").serialize();
console.log(datastring);
$.ajax({
type: "POST",
url: location.origin+"/user/parsonal_image_submit/",
secureuri :false,
fileElementId :'user_image',
data: datastring,
dataType: "json",
success: function(data) {
//success },
error: function() {
//error
}
});
})

Check below code. Hope it will work for you. Please use on form submit rather then using on click.
$('#your_form_id').on('submit',function(e){
e.preventDefault();
var formdata = new FormData($(this)[0]);
var url = $('#personal_image').attr('action');
$.ajax({
url: url,
type: 'post',
data: formdata,
dataType: 'json',
processData: false,
contentType: false,
//async: false,
success: function(data){
//success
}
});
});

Related

send additonal data with form data via ajax to php

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) { }
});
});

How can i solve Ajax Ckeditor value post problem

I am trying to send a CKeditor value post with ajax but i cant response anyway! I cant find anything
function send_days(tourId){
var url = baseUrl + "tour/save_days/" + tourId;
var value = CKEDITOR.instances.tour_textarea_days.getData();
$.ajax({
url: url,
method: "POST",
data: dataString,
contentType: false,
processData: false,
cache:false,
success: function (data) {
$('.tour_popup_container').html(data);
}
});
}
but when i chance ajax method like this. It is succesfull
$.post(url, {data:value}, function (response) {
$('.tour_popup_container').html(response);
})
here is my codeigniter php file (it is not important actually)
public function save_days($tourId)
{
$value=$this->input->post("data");
print_r($value);
}
It looks like you used dataString instead of value.
var url = baseUrl + "tour/save_days/" + tourId;
var value = CKEDITOR.instances.tour_textarea_days.getData();
$.ajax({
url: url,
method: "POST",
data: value /*dataString*/,
contentType: false,
processData: false,
cache:false,
success: function (data) {
$('.tour_popup_container').html(data);
}
});

Posting JSON with variable using Ajax in [duplicate]

This question already has answers here:
jQuery posting valid json in request body
(2 answers)
Closed 4 years ago.
I was trying to put variable into JSON. I want to post it using Ajax.
My code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var user_Details = "1528205024";
function checkUserForDashboard(){
$.ajax({
url: "api comes here",
type: "POST",
data: {"user_id": user_details },
dataType: "json",
crossDomain : true,
success: function (data) {
console.log(data);
}
})};
</script>
The post request gives: bad request error.
Enclose your JSON object into JSON.stringify() to ensure your json object is serialized in a safe string.
Also, set the content-type property.
$.ajax({
url: "api comes here",
type: "POST",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({"user_id": user_details }),
dataType: "json",
crossDomain : true,
success: function (data) {
console.log(data);
}
})};

How to add PHP Session variable into FormData using AJAX?

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.

Transmitting Form Data via Json

I believe I am making a very basic mistake somewhere.
I have a Form I want to transmit to a PHP page. I also want to send a parameter with that information so I have created a basic 2D array:
$fd['api'] -> contaning the parameter as a string
$fd['body'] -> containing the form data
I am struggling to transmit this array "$fd" as a json string and believe I am using the javascript syntax incorrectly somewhere as I do not often use Javascript.
Any Help would be appreciated.
function admin_statistics_form_send(){
var fd = []
fd['api'] = "refresh_all"
fd['body'] = new FormData(document.getElementById("admin_statistics_form"))
var jsonstring = fd
console.log(jsonstring)
$.ajax({
async: true,
beforeSend: function(){
},
url: "admin_statistics_api.php",
type: "POST",
data: jsonstring,
dataType: "json",
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function (data) {
console.log(data)
},
error: function(data) {
console.log(data)
}
})
}
You only want to send the FormData object. To add other key/value pairs you append to that object:
function admin_statistics_form_send(){
var fd = new FormData($("#admin_statistics_form")[0]);
fd.append('api',"refresh_all");
$.ajax({
//async: true, // redundant since it is default and should never use `false`
beforeSend: function(){
},
url: "admin_statistics_api.php",
type: "POST",
data: fd,
dataType: "json",
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function (data) {
console.log(data)
},
error: function(data) {
console.log(data)
}
})
}

Categories

Resources