how to get Ajax Success Function data in codeigniter - php

my ajax code
$.ajax({
url : '<?php echo base_url('mobile_plan/getStates')?>',
type: 'POST',
data:{operator:operator},
dataType:'json',
success: function(response)
{
alert(response);
}
});
I am getting response in array for from controller now this ajax call is done from the same page in which I want response back in PHP array for further operating could you please guide me how it is possible. I am using codeigniter 3.1.

If you want to send this response again to the controller in php you should create another ajax call with POST method and pass the response array as a data parameter.

When using ajax it's recommended to pass data in JSON format.
In PHP you should do
echo json_endoce($data);
And in Javascript, in your success function
response = JSON.parse(response);

Related

POST ajax json response to PHP

I am looking for get json response from third party website. Its providing me json data when I call with ajax. Its fine. I am looking for pass same json data to my PHP file. so I can decode that json data and can store in MYSQL database. So for I am trying like this
<script>
$("#diary").click(function(){
$.ajax({
type:'get',
url:'https://example.com,
data:{
},
dataType:'json',
success:function(result){
console.log(result);
$.ajax({
type: "POST",
url: "dd.php",
data:result,
dataType: "json",
success: function (msg) {
console.log(msg);
}
});
}
});
});
</script>
Its working for get data from third party site but in my php page, I am not receiving proper data so json_decode function not working. May be I am not posting correct json data to PHP page. I am not able to use CURL in PHP because its giving me connection error, its possible that third party site have some security function which does not allow me to connect via CURL so ajax is the only method for get data.
My PHP file is like below
<?php
$ajax = json_decode(file_get_contents('php://input'), true);
print_r($ajax);
?>
Let me know if anyone here can help me for solve my issue.
Thanks!
You need to call JSON.stringify() on the result and pass that through data in your ajax request. You are just sending a string that happens to be JSON data. In PHP you can call json_decode() on $_POST['data'] and you should have your data.

Controller behaviour with ajax request

I am trying to get a hold on sending data to MySql via ajax and have been watching online tutorials. In the examples, the controller method always seems to end with an echo statement which is returned to the js script. Under other circumstances, if I put an echo statement in a controller method it would be output to the view so why does this not happen after an ajax request?
ajax works with js, and the response by ajax request can only be handle through js.
Reason => after generating ajax response on server, it bounce back to client/browser, where server side language doesn't work, so you need to manage your code/logic through client side language JS in your ajax success block.
$.ajax({
url: 'content/get.php',
type: 'post', // performing a POST request
data : {
data1 : 'value' // will be accessible in $_POST['data1']
},
dataType: 'json',
success: function(data)
{
// success block
}
});

PHP: Multiple ajax request

I want a code for multiple ajax request. What happens actually is my first ajax request give me the response and in that responce function i m calling another function which having another website url. I want to send data to this new website using multiple ajax request.
Please help me out...
Thanks,
Prafulla
use global variables and manipulate your data in each request.
like:
var a;
$.ajax({
url:url_one;
success: function(data){ a =data; }
});
$.ajax({
url:url_two;
data: a //sends the data from the 1st request
success: function(data){
//do something with the data from the 2nd url
}
});
You can encapsulate your ajax call in function or event handlers as you need.
You can also manuipulate the data returned in variable a before sending it to the 2nd url.
Seems sloppy to me, but sould work.

AJAX, POST, JSON, and PHP: How do I do it?

How do I send AJAX data through $.ajax() in JavaScript via type: "POST" using JSON data formatting and how do I receive the data in a PHP script (through $_POST??) and put it into an array so I can use it? I've been kicking at this for hours and I have no idea what I'm doing wrong. If someone could post the JS and PHP code for sending and receiving JSON formatted data, I would be eternally grateful!!!!!
JS Code:
$.ajax({
type: "POST",
url: $(location).attr('protocol') + "//" + $(location).attr('hostname') + "/ajax/rate.php",
data: {
"value1": 1,
"value2": 2,
"value3": 3,
"value4": 4,
"value5": 5
},
dataType: "json"
});
PHP Code:
I was just using $_POST["value1"], etc., to get that value. On that note, is there a way to make the ajax request GET instead AND open up a new window with that GET data on it so I can see what's going on??
The idea is to create a php page the outputs data in JSON form. This data is taken from an array and echoed using the json_encode function. Using the $.ajax() method from jQuery, you send a request to that page and manipulate the data in the success: function.
Example.
PHP - array.php
$array = ("flag" => 1);
echo json_encode($array);
JavaScript
$.ajax({
url : '/array.php', // page containing JSON data
dataType : 'json', // must be specified for JSON manipulation in success
success : function(data) {
// this function is called if the call to test.php is successful
// access the data using object dot syntax
alert(data.flag); // should display '1'
}
});
// Send data to server this way
PHP - test.php
echo $_POST['data'];
JavaScript
$.ajax({
url : '/test.php',
dataType : 'text',
type : 'post',
data : { data : 'Hello, World!'},
success : function(data)
alert(data); // should display 'Hello, World'
}
});
As far as I know you can't POST data in a JSON format. Only as a query string, like GET. You can however return data from the PHP script in a JSON format.
Eg.
$.ajax({
url: "script.php",
dataType: 'json', // Tell jQuery/JS that the returned data from script.php is JSON format
data: 'id='+Id, // will become $_POST['id'] with the value of Id (js var)
success: function(data){
// data is JSON formatted
}
Now, in your PHP script, you receive a POST variable called $_POST['id'].
Let's say that $_POST['id'] is needed to request a certain customer from the database. Its data can be stored in an array and then json encoded and then send back to the page with the ajax request.

How can I implement a jquery ajax form which requests information from a web api via a php request?

I'm trying to implement a simple api request to the SEOmoz linkscape api. It works if I only use the php file but I want to do an ajax request using jquery to make it faster and more user friendly. Here is the javascript code I'm trying to use:
$(document).ready(function(){
$('#submit').click(function() {
var url=$('#url').val();
$.ajax({
type: "POST",
url: "api_sample.php",
data: url,
cache: false,
success: function(html){
$("#results").append(html);
}
});
});
});
And here is the part of the php file where it takes the value:
$objectURL = $_POST['url'];
I've been working on it all day and can't seem to find the answer... I know the php code works as it returns a valid json object and displays correctly when I do it that way. I just can't get the ajax to show anything at all!!
...data: 'url='+encodeURIComponent(url),

Categories