Codeigniter: Ajax Request, global variable in controller - php

I setting the global variable in the __construct();
function __construct()
{
parent::__construct();
//variables
$this->galleryID = $this->uri->segment(3);
$this->productID = $this->uri->segment(4);
}
After a selection is made from a dropdown menu I make an ajax request.
$.ajax(
{
type: 'POST',
url: '/beta/checkout/getCoverSizes',
data: {
column: size
},
dataType: 'json',
success: function (json)
{
console.log(json);
}
});
And at this point, simply output the global variable
public function getCoverSizes()
{
print_r($this->productID);
}
Currently nothing is $this->productID is returning 0, and I am positive that it is correct as function index() depends on this variable and is rendering the data correctly. The ajax request does not appear to be accessing the global variable $this->productID.

$.ajax(
{
type: 'GET', // use GET here
url: '/beta/checkout/getCoverSizes',
data: {
column: size
},
dataType: 'json',
success: function (json)
{
console.log(json);
}
});

You are using $this->uri->segment(3); for galleryID and $this->uri->segment(4); for productID but the url in ajax call doesn't have these parameters you should pass these ids in ajax call in order to get like
$.ajax(
{
type: 'POST',
url: '/beta/checkout/getCoverSizes/1/2',
//url: '/beta/checkout/getCoverSizes/galleryID/productID',
data: {
column: size
},
dataType: 'json',
success: function (json)
{
console.log(json);
}
});
And in your class i assume you have define the global variables like
class checkout extends CI_Controller {
public $galleryID;
public $productID;
// your other code
}

in java-script pass the value js to globle ajax
$("#abc").on("change",function(){
var post_data = new FormData();
ajax_request("dashboard/ajax",post_data, response,null);
});
and then in JS
function ajax_request(URL, request_data, response_function, element){
$.ajax({
type: "POST",
datatype:"json",
url: BASE_URL+URL,
data: request_data,
mimeType: "multipart/form-data",
contentType: false,
cache: false,
processData: false,
success: function(result){
response_function(JSON.parse(result), element);
},
error: function() {
response_function(undefined, element);
}
});
}

Related

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');

Simple ajax call in wordpress for upvoting post

Here's my ajax call:
$("#yes, #no").click(function(){
var upOrDown = $(this).attr('id');
$.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
action: "updateVote",
data: {upOrDown: upOrDown},
dataType: "json",
success: function(data) {
console.log(data.output);
}
});
return false;
});
Here's the function in functions.php
function updateVote() {
echo json_encode(array(
'output' => 'hello!'
));
die();
}
add_action('wp_ajax_updateVote', 'updateVote');
add_action('wp_ajax_nopriv_updateVote', 'updateVote');
Why does this keep returning "0". It should return "hello!"
Thanks.

Call a jQuery.ajax action function from inside a class

function submit_onClick() {
var addValue = jQuery("#billing_address_1").val();
jQuery.ajax({
type: "POST",
url: "http://localhost/wordpress/wp-admin/admin-ajax.php",
data: {'action':'distance_calculation', addValue:addValue}
}).done(function(data) {
alert(data);
});
}
this works fine until I dont place distance_calculation inside any class.
how can i call a function if its inside any class for example, if distance_calculation function is inside any class called "distance" then how to call this in action .
Have the the ajax request like so
function submit_onClick() {
var addValue = jQuery("#billing_address_1").val();
jQuery.ajax({
type: "POST",
url: "http://localhost/wordpress/wp-admin/admin-ajax.php?method=distance_calculation&addValue=addValue"
}).done(function(data) {
alert(data);
});
}
Then in your PHP file add
if(strcmp($_POST["method"],"distance_calculation")){
$class->distance_calculation($_POST["addValue"]);
}

Get result PHP code in jQuery function?

I use codeigniter and i want make uniqid code with php as following code(by random_string()):
function coding(){
echo random_string('unique');
}
I want use result top function in a, jquery function (for use in js code), i in jquery code try as this, but this output in alert(coding()) is "undefined". How can fix it?
function coding(){
$.ajax({
type: "POST",
dataType: "text",
url: "coding",
cache: false,
success: function (data) {
return data;
}
})
}
alert(coding()) // in here output is "undefined" !!!?
function coding(){
var temp;
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
async: false,
success: function (data) {
temp = data;
}
})
return temp;
}
alert(coding());
You will receive an [object XMLDocument].
It is becuse coding function is asynchronous.
Use follow code:
function coding(){
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
success: function (data) {
alert(data);
}
})
}
coding();
function coding(){
$.ajax({
type: "POST",
dataType: "text",
url: "coding",
cache: false,
success: function (data) {
$("#yourDiv").html(data);
}
})
}
You can assign value to another element like div, p etc.
Well, I'm assuming that the coding variable you are passing at the parameter url is a reference to a method in your CI controller.
If that's the case, you just need to wait from the success callback:
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
success: function (data) {
alert(data);
}
})
Ok, you should use callback function like this:
function coding(func){
$.ajax({
type: "POST",
dataType: "text",
url: coding,
cache: false,
success: function (data) {
func(data);
}
})
}
coding(function(data)
{
alert(data);
});

JS jquery and ajax problem

Hi i have problem with some things
Here is the code
function get_char_val(merk) {
$.ajax({
type: "POST",
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){return(data);}
});
}
alert(get_char_val("str"));
when alert comes out it's output the undefined please help fast i'm making this two days xC
get_char_val does not return anything. The success callback needs to have alert(data) in it to return the data from AJAX.
$.ajax is asynchronous - meaning it doesn't happen in order with other code, that is why the callback exists.
Your return; statement will return the value to the anonymous function you pass into the success handler. You cannot return a value like this, you need to invoke another callback instead.
function get_char_val(merk, cb) {
$.ajax({
type: "POST",
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){cb.apply(this, data);}
});
}
get_char_val("str", function(data) {
alert(data);
});
You can either set the async:false config parameter in the ajax call options or use the call back.
Using Async:false -
function get_char_val(merk)
{
var returnValue = null;
$.ajax
(
{
type: "POST",
async:false,
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){returnValue = data;}
}
);
return returnValue;
}
alert(get_char_val("str"));
P.S: Note that making ajax calls synchronous is not advisable.
Since you do an asynchronous ajax call, the response of the server is handled in the "success" function. The return in your success function is useless, you should use the "data" parameter directly in the function
This should work. Use it like this.
function get_char_val(merk) {
var myReturnData= "";
$.ajax({
type: "POST",
url: "char_info2.php",
data: { name: merk },
dataType: "html",
success: function(data){myReturnData = data;}
});
return myReturnData;
}
alert(get_char_val("str"));
Just understand the problem with your piece of code. Use the return statement under the right function.

Categories