Jquery - Send variables to a controller Action via GET in AJAX - php

All,
I want to send a variable "itemId" via GET to a controller action through AJAX. In the Controller Action, I should be able to retrieve the value using $_GET["itemId"];
Can I send the querystring with "data" tag instead of appending it to the "url"?
I have the following code:
$.ajax({
type: 'GET',
url: "/controller/controlleraction",
data: itemId,
cache: false,
dataType: "html",
success: function(html_input)
{
alert(html_input);
}
});
How can I do this?

data: {itemId: itemId},

$.ajax({
type: 'GET',
url: "/controller/controlleraction",
data: ({itemId: itemId}),<------change it to this
cache: false,
dataType: "html",
success: function(html_input)
{
alert(html_input);
}
});

Make itemId a JavaScript object before making the AJAX request. For example:
var itemId = {'itemId': 1000};

data: {itemId: "you info"},
or
data: "itemId=you info",

Related

Sending ID data with jQuery AJAX

I need to fetch my id and Edit the tables value..but i am not able to fetch id from database and actually i am trying to send it by this way....
$(document).on('submit', '#form_company.edit', function(e){
e.preventDefault();
// Validate form
if (form_company.valid() == true){
// Send company information to database
hide_ipad_keyboard();
hide_lightbox();
show_loading_message();
var id = $('#form_company').attr('data-id');
var form_data = $('#form_company').serialize();
var request =
$.ajax({
url: 'data.php?job=edit_company&id=' + id,
cache: false,
data: form_data,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'get'
});
`
but it is getting passed by URL ..i want to send it by "data:"..how to do it..
You can post both data in POST parameter like below:-
$.ajax({
url: 'data.php',
type: 'POST',
cache: false,
data: {'form_data':form_data,'job':'get_company','id': id} ,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
});
And now in php (data.php):-
<?php
echo"<pre/>";print_r($_POST);
?>
Note:- based on this printed data you can do your stuff accordingly. Thanks
You are using type as get that's why it is passing data by url. You should use POST if not want to pass data in URL
$.ajax({
url: 'data.php?job=get_company',
type: 'POST',
cache: false,
data: {id: id} ,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
});
Check my changes and hope this would be helpful to you.
$(document).on('submit', '#form_company.edit', function(e){
e.preventDefault();
// Validate form
if (form_company.valid() == true){
// Send company information to database
hide_ipad_keyboard();
hide_lightbox();
show_loading_message();
var form_data = $('#form_company').serialize();
form_data.append('id',$('#form_company').attr('data-id'));
form_data.append('job',"edit_company");
var request= $.ajax({
url: 'data.php',
cache: false,
data: form_data,
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'post'
});
Try a post request with the following:
$.ajax({
url: 'data.php',
cache: false,
data: {form_data:form_data,job:'edit_company',id:id},
dataType: 'json',
contentType: 'application/json; charset=utf-8',
type: 'POST',
success: function(data){
}
});

How to retrieve parameter passed from ajax to controller in Codeigniter

I want to retrieve parameter passes from ajax in Controller in Codeigniter but did not know how to use it my ajax function is below
function sendVideoData(frm_id)
{
var data = new FormData(document.getElementById("post_video_"+frm_id));
// make the AJAX request
jQuery.ajax({
type: "POST",
url: "<?php echo base_url(); ?>"+"dashboard/do_upload",
data: data+'&form_id='+frm_id,
mimeType:"multipart/form-data",
contentType: false,
cache: false,
processData:false,
dataType: 'json',
success: function (data) {
alert(data);
},
});
return false;
}
</script>
I want to retrieve
form_id
in my controller and use it
It's pretty simple,
echo $this->input->post('form_id')
or simple php
echo $_POST['form_id']
inside your controller dashboard and method do_upload
You can get all list of post variables using print_r($this->input->post()) as well.
Your AJAX request can be:
jQuery.ajax({
type: "POST",
url: "<?php echo base_url() ?>dashboard/do_upload",
data: 'form_id=' + frm_id + '&data=' + data,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData:false,
dataType: 'json',
success: function (data) {
alert(data);
},
});
And in ur controller: $this->input->post('form_id');

how to convert $.post form to $.ajax form?

I am noob at using jquery and ajax. I need to change the form from $.post to $.ajax .
var disqus_config = function() {
this.callbacks.onNewComment = [function(comment) {
$.post("sendnotification", { comment: comment.id, post: $post->id,author:$author->id}, function(result){
alert(result);
});
}];
};
I know I need to end something like here but I am stuck how to use post datas(comment,post,author) inside this function
$.ajax({
url: 'sendnotification',
type: 'POST',
data: 'query=' + query ,
dataType: 'JSON',
async: true,
success: function(data){
process(data)
}
Thanks
Just use the same object literal you did for $.post, eg (gotta assume that's some PHP or something in there)
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: {$post->id}, author: {$author->id} },
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});
I believe dataType: 'JSON' should be changed to dataType: 'json'
Also, use the same data array as you used in your $.post variant.
$.ajax({
url: 'sendnotification',
type: 'POST',
data: { comment: comment.id, post: $post->id,author:$author->id } ,
dataType: 'json',
async: true,
success: function(data){
process(data)
}
});

Knockout and AJAX post request PHP

I am attempting to get our knockout form to submit to a php script and am getting undefinedIndex errors. I am pretty sure it is the way we are sending the data over in our ajax function.
Here is the ajax:
$.ajax({
url: '/orders/add',
type: 'post',
data: {payload:ko.toJSON(allModel)},
contentType: 'application/json',
success: function (result) {
alert(result);
}
});
Here is the PHP (we use laravel)
return json_decode($_POST["payload"]);
Pete is correct. You need to use just one data field. If you want a variable, define it before the $.ajax post
var dataPayload = ko.toJSON(allModel);
$.ajax({
url: '/orders/add',
type: 'post',
data: {payload: dataPayload},
contentType: 'application/json',
success: function (result) {
alert(result);
}
});

Send and receive data in same ajax request with jquery

What is the best way to send data and receive a response dependent on that data?
Consider the PHP file used for the request:
$test = $_POST['test'];
echo json_encode($test);
I have tried unsucessfully to achieve this with:
$.ajax({
type: "POST",
dataType: "json",
data: '{test : worked}',
url: 'ajax/getDude.php',
success: function(response) {
alert(response);
}
});
Lose the quotes to pass the object:
$.ajax({
type: "POST",
dataType: "json",
data: {test : worked},
url: 'ajax/getDude.php',
success: function(data) {
alert(data);
}
});
Instead of this
data: '{test : worked}'
try
data: {"test" : worked} // Worked being your data you want to pass..
data: {"test" : "worked"} // Else enclose worked in quotes
The problem appears to be that you're submitting a string rather than a json object - change data: '{test : worked}' to data: {test : 'worked'}

Categories