type1 .. working
function aaa (frm_id){ var formData = new FormData($("#"+frm_id)\[0\]);var html_source = "";//console.log(formData); $.ajax({ cache : false, url : "a.php", type : 'POST',data : formData, async: false,contentType: false, processData: false,success : function(data) { .... } }
type2 .. not working this code...
function aaa (frm_id){ //var formData = new FormData($("#"+frm_id)\[0\]);var html_source = ""; var formData = $("#"+frm_id).serialize(); console.log(formData); $.ajax({ cache : false, url : "a.php", type : 'POST',data : formData, async: false,contentType: false, processData: false,success : function(data) { .... } }
i'm testing 2 type code a.php received page print_r($_POST) run type1 code is work normally but type2 not working...
type2 before the submit console.log result is..prc_key=U3d2YUJZT0FyN3VOTHlOOS82UlYrZz09&prc_type=WlBoMnpVb2RaWUZZZUgzdGV4dzAyQT09&pagenum=1&bh_cate_section=&search_key=
but a.php print_r($_POST); is empty..
i don't know reason.... tell me please..
Related
I have to submit data with attachment using java scripts and PHP, my problem is that I cant pass data and attachment to php page. here is my codes;
In my HTML i did not use
function kk_sendmail()
{
var kk_vtype= $("#vtype").val();
var kk_departm= $("#departm").val();
var kk_recepient= $("#recepient").val();
var kk_user= $("#euser").val();
var kk_smssubject= $("#smssubject").val();
var kk_compose_textarea= $("#compose-textarea").val();
var form_data = new FormData();
form_data.append('attach', $('#attachments').prop('files')[0]);
form_data.append('vtype',kk_vtype);
form_data.append('departm',kk_departm);
form_data.append('recepient',kk_recepient);
form_data.append('euser',kk_user);
form_data.append('smssubject',kk_smssubject);
form_data.append('compose-textarea',kk_compose_textarea);
$.ajax({
type: "POST",
url: "process.php",
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: form_data,
success : function(text){
if (text == "success"){
formSuccess();
} else {
console.log('Failed message');
}
}
});
};
you do not need to define the dataType and also you can use
enctype: "multipart/form-data",
$('#attachements')[0].files[0] // for file
It will help
This question already has answers here:
Ajax Upload image
(6 answers)
Closed 8 months ago.
In Codeigniter by using ajax i am adding record to (products) and everything is working fine so i decided to add (image) field , and for some reason it's no longer adding any record to database
and i add input type=file to my form
<input type="file" name="image">
and i add this to my controller
$image = $this->input->post('image');
$data = array('title'=>$title,'description'=>$description,'price'=>$price,'quantity'=>$quantity,'image'=>$image);
but when i remove $image = $this->input->post('image'); it gets to work again
just in case this is my ajax code
var postData = $(this).serialize();
$.ajax({
type:'post',
url: baseURL+"admin/Products/add_product",
data:postData,
dataType:'json',
success:function(data){
}
});
any ideas how to solve it?
Hope this will help you :
Your ajax should be like this :
var formdata = new FormData();
$.ajax({
type: 'POST',
url: baseURL+"admin/Products/add_product",
data: formdata,
cache: false,
contentType: false,
processData: false,
success: function(response)
{
alert(response);
}
});
In controller accessing image using $_FILES super variable
public function add_product()
{
print_r($_FILES);
print_r($this->input->post());
exit;
}
Note : make sure URL path is correct , see ur network tab to see the output
For more : https://www.formget.com/ajax-image-upload-php/
May Be You can use instead of baseURL
var formdata = new FormData();
$.ajax({
type: 'POST',
url: <?=base_url()?>+"admin/Products/add_product",
data: formdata,
cache: false,
contentType: false,
processData: false,
success: function(response)
{
alert(response);
}
});
I am trying to display this "echo $_FILES['userfile']['name'];" on browser console but unfortunately I got this "function File() { [native code] }"
Here is my jquery code
<?= form_open_multipart('',' id="importform" method="POST" ');?>
<div><input type="file" name="userfile"></div><button type="submit>upload</button>
var formdata = new FormData();
formdata.set('userfile',$('input[name="userfile"]')[0].files[0],File);
$.ajax({
url:'http://localhost/selection/index.php/CI_Inner/importResult',
type: 'POST',
dataType: 'html',
contentType: false,
processData: false,
data: formdata,
success: function(data){
console.log(data);
}
Try this
function uploadImage() {
// send the formData
var formData = new FormData( $("#userfile")[0] );
if (typeof formData !== 'undefined') {
$.ajax({
url : 'http://localhost/selection/index.php/CI_Inner/importResult', // Controller URL
type : 'POST',
data : formData,
async : false,
cache : false,
contentType : false,
processData : false,
success : function(data) {
successFunction(data);
}
});
} else {
message("Your Browser Don't support FormData API! Use IE 10 or Above!");
}
}
Note: instead of 'http://localhost/selection/index.php/CI_Inner/importResult' using direct URL baseUrl + 'importResult',
example: url :
'http://localhost/selection/index.php/CI_Inner/importResult',
url: baseUrl + 'importResult',
Eventually I solved this by just removing the parameter 'File' from my code below
var formdata = new FormData();
formdata.set('userfile',$('input[name="userfile"]')[0].files[0],**File**);
$.ajax({
url:'http://localhost/selection/index.php/CI_Inner/importResult',
type: 'POST',
dataType: 'html',
contentType: false,
processData: false,
data: formdata,
success: function(data){
console.log(data);
}
And now it works fine with the code below
var formdata = new FormData();
formdata.set('userfile',$('input[type=file]')[0].files[0]);
$.ajax({
url : 'http://localhost/selection/index.php/CI_Inner/importResult',
type: 'POST',
dataType: 'html',
contentType: false,
processData: false,
data: formdata ,
success: function(data){
console.log(data);
}
I can post only image and get it with $_FILES['foto']['name']. I need post image and text same time.
var fotoFile = new FormData();
$('#foto').on('change', function (evt) {
var files = evt.target.files;
if (files.length > 0) {
fotoFile.append("foto", files[0]);
}
});
It is post code
` $.ajax({
url: 'postpages/personelsave.php',
dataType: 'text',
type: 'post',
cache: false,
contentType: false,
processData: false,
data: {foto : fotoFile, tc_no : document.getElementById('tcNo').value},
success: function(php_script_response){
alert(php_script_response);
}
});`
and personelsave.php
$_FILES['foto']['type']
$_POST["tc_no"]
Error : undefined index foto.
What is wrong with it?
You can't use multiple dataTypes, if you use JSONP this will return a jsonp block which you could use to call a callback to handle the return data like this:
Basic example of using .ajax() with JSONP?
So through JSONP you can handle multiple dataTypes.
Just use below to submit all types of input data including file
var formData = new FormData($("#formID")[0]);
$.ajax({
type: "POST",
url: 'postpages/personelsave.php',
data: formData,
processData: false,
contentType: false,
});
I've got an array like this one :
var cars = new Array();
cars['mom'] = "Ford";
cars['dad'] = "Honda";
I want to send the array cars via jQuery .ajax() function :
var oDate = new Date();
$.ajaxSetup({
cache: false
});
$.ajaxSetup({ scriptCharset: "utf-8" ,contentType: "application/x-www-form-urlencoded; charset=UTF-8" });
$.ajax({
url: path+'/inc/ajax/cars.php',
data: {cars:cars},
cache: false,
type: "POST",
success : function(text){
alert(text);
}
});
How can I read the array on the server side, with my PHP script ?
Thanks !
try using php special array $_POST
$php_array = json_decode($_POST['cars']);
$.ajax({
url: path+'/inc/ajax/cars.php',
data: {cars:$(cars).serializeArray()},
cache: false,
type: "POST",
success : function(text){
alert(text);
}
});
php
$arr = $_POST['cars'];