How to send String data with formdata in ajax - javascript

<script type="text/javascript">
$(document).ready(function(){
$("#btnUpdate").click(function(){
alert($("#frm_data").serialize());
var formData = new FormData($("#frm_data")[0]);
var Desc= CKEDITOR.instances.editor1.getData();
$("#btnUpdate").attr('value', 'Please Wait...');
$.ajax({
url: 'update_job.php',
data: formData,
cache: false,
contentType:false,
processData:false,
type: 'post',
success: function(response)
{
$("#btnUpdate").attr('value', 'Update');
}
});
return false;
});
})
</script>
i use ckeditor for textarea field. but its can update value with new value, so i want to use another way with send textarea value with form data.
so how to send Desc data with fromData. in ajax.

To achieve this you can use the append() method of FormData to add whatever additional information you require:
$("#btnUpdate").click(function(e) {
e.preventDefault();
var $btn = $(this).attr('value', 'Please Wait...');
var formData = new FormData($("#frm_data")[0]);
formData.append('desc', CKEDITOR.instances.editor1.getData());
$.ajax({
url: 'update_job.php',
data: formData,
cache: false,
contentType: false,
processData: false,
type: 'post',
success: function(response) {
$btn.attr('value', 'Update');
}
});
});

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

Data is not display after insertion through Bootstrap modal with jquery ajax in php Mysqli

Data is not display after insert through bootstrap modal with ajax in php mysqli.
jQuery Ajax code:
$(document).ready(function() {
fetch_data();
function fetch_data() {
var action = "fetch";
$.ajax({
url: 'action.php',
method: 'post',
data: {action: action},
success: function (data) {
$("#all_table_data").html(data);
}
});
}
});
function addData() {
var formData = new FormData($('#employee_insert_form')[0]);
formData.append('action', 'add');
$.ajax({
method: 'post',
url: 'action.php',
data: formData,
processData: false,
contentType: false,
cache: false,
success: function (response) {
$("#all_table_data").html(response);
$('#empInsert').modal('hide');
fetch_data();
}
});
}
Your PHP code never return anything to your view. So try to echo result of select * from tbl_employee after the insertion

i am trying to pass parameter to data through ajax

I am trying to pass value through an ajax json array but value of catergory variable is not getting in controller action
var category = $('#category').val();
var url = $('#ajax_action_search').val();
$.ajax({
type: "POST",
data: {
'category': category
},
dataType: "json",
cache: false,
contentType: false,
processData: false,
success: function(response) {}
});
You need to use the parameter namespace matching your extension/plugin:
$.ajax({
// ...
data: {
'tx_myext_foo[category]': category,
},
// ...
});
But you'll also need to configure the cHash evaluation since this will lead to a HTTP request like /?tx_myext_foo[category]=X which will fail without a matching cHash.
This can be done with the excludedParameters configuration option.
Please check the Controller action if category (parameter name) passed from the ajax is exactly same in the controller action too
var category = $('#category').val();
var url = $('#ajax_action_search').val();
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
cache: false,
async: false,
url: url,
data: JSON.stringify {
category: category
},
dataType: 'json',
success: function(response) {}
});
You need to make ajaxurl with action and controller. and then pass the data in full format.
var ajaxUrl = '<f:uri.action action="youraction" controller="Yourcontroller" />';
var category = $('#category').val();
$.ajax({
type: 'POST',
url: ajaxUrl,
data: '&tx_yourext_yourplugin[category]='+category,
success: function(response) {
},
});
Just make the following changes :
var category = $('#category').val();
var url = $('#ajax_action_search').val();
$.ajax({
type: "POST",
url:url,
data: {'category': category},
dataType: "json",
success: function(response) {}
});

Append additional variable to formData

How do I append dataid to the formData so that AJAX POST's both? I have tried formData.append('id', dataid); and formData = formData.append('id', dataid);
$(document).ready(function() {
$('#insert_screen').on("submit", function(event) {
var dataid = $("#res option:selected").attr('data-value');
console.log("Value", dataid);
event.preventDefault();
var form = $('form')[2];
var formData = new FormData(form);
$.ajax({
url: "insert_new_screen.php",
data: formData,
method: "POST",
cache: false,
contentType: false,
processData: false,
beforeSend: function() {
$('#insert').val("Inserting");
},
success: function(data) {
$('#add_screen_modal').modal('hide');
window.location.reload();
}
});
});
});
UPDATE:
Below is the code I managed to get working:
$(document).ready(function(){
$('#insert_screen').on("submit", function(event){
var dataid = $("#res option:selected").attr('data-value');
console.log("Value", dataid);
event.preventDefault();
var form = $('form')[2];
var formData = new FormData(form);
formData.append("RecordID", dataid);
$.ajax({
url:"insert_new_screen.php",
data: formData,
method:"POST",
cache: false,
contentType: false,
processData: false,
beforeSend:function(){
$('#insert').val("Inserting");
},
success:function(data){
$('#add_screen_modal').modal('hide');
window.location.reload();
}
});
});
});
Many thanks to all those who gave me help. I hope this helps other.

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.

Categories

Resources