I want to build a graph in jqchart where i need to get two arrays
Now i want to perform operation as given below.Which is giving error ofcourse.
html
$.ajax(
{
type: "GET",
url: "customer_coverage.php",
data: {id:id},
contentType: "application/json",
dataType: "json",
success: function (data21,data22) {
initChart2(data21,data22);
}
});
function initChart2(data21,data22) {
$('#jqChart2').jqChart({
series: [
{
type: 'column',
title: 'no of days ',
data:data21,
},
{
type: 'column',
title: 'no of days ',
data:data22,
},
]
});
}
heres PHP code
echo json_encode($arr1);
echo json_encode($arr2);
So any one has idea of how to do it?
no need to echo json encode two times....merge the array and send the data.......
echo json_encode(array('result1'=>$arr1,'result2'=>$arr2));
and get data by
initChart2(data.result1,data.result2);
See if you are able to produce two object arrays of json then you can try with this:
var data21,data22;
$.ajax({
type: "GET",
url: "customer_coverage.php",
data: {id:id},
contentType: "application/json",
dataType: "json",
success: function (data) {
$.each(data, function(i, item){
data21 = item.data21;
data22 = item.data22;
});
initChart2(data21,data22);
}
});
and i am supposing if you are able to produce this:
[
{
"data21": {
.........
},
"data22": {
........
}
}
]
You cannot get multiple object like that. For a JSON object, you will need to have single object. So what you can do is, create a wrapper object put those two array inside it.
So basically, your php code will be:
<?php
$arr= array();
$arr['arr1'] = $arr1;
$arr['arr2'] = $arr2;
echo json_encode($arr);
?>
So now you will have single main array and so single JSON object.
On JS side, you will get single data. Little Modification will be
$.ajax(
{
type: "GET",
url: "customer_coverage.php",
data: {id:id},
contentType: "application/json",
dataType: "json",
success: function (data) {
var data21=data['arr1'];
var data22=data['arr2'];
initChart2(data21,data22);
}
});
This should work.
You need to combine both array using array_merge().
Example
$response = array();
$response = array_merge($arr1,$arr2);
echo json_encode($response);
Related
I want to post some data to php function by ajax, then get the encoded json object that the php function will return, then I want to get the information (keys and values) from this object, but I don't know how, here is my code:
$.ajax({
url: "functions.php",
dataType: "JSON",
data: {id: id},
type: 'POST',
success: function(json){
for(var i=0;i<json.length;i++){
alert(json['fname']);
}
}
});
and here is the json object returned:
[{"id":"1","fname":"kjhkj","mname":"kjhjh","lname":"lname","prefix":"Mr.","suffix":"jhkjhk","email":"hf#dd.com","image":"11281454_423648214427141_318277024_o.jpg","info":"hjgvhd"}]
Try:
$.ajax({
url: "functions.php",
dataType: "JSON",
data: {id: id},
type: 'POST',
success: function(json){
for(var i=0;i<json.length;i++){
alert(json[i].fname);
}
}
});
It is rather simple to do this:
var data = jQuery.parseJSON(json);
jQuery.each(data, function(i, item) {
jQuery('.derp').append(item.mname + "<br />");
});
Example
Reference
jQuery.each()
jQuery.parseJSON()
What i want to do: copy all selected rows from FIRST table and insert them into SECOND table. In json im sending event type ( insert or delete) and array of IDs rows to copy.
How i can acces to this JSON data in PHP file?
JS:
var data_to_send = {
event : "accept",
ids : ids_to_accept,
}
console.log(ids_to_accept);
$.ajax({
type: "POST",
url: "request.php",
contentType: "application/json; charset=utf-8",
data: data_to_send,
success: function(response){
console.log("Dodałem rekord!");
},
});
PHP:
<?php
//include db configuration file
include_once("connect.php");
if(isset($_POST["data"])){
$data = $_POST["data"];
$ids_to_insert = array();
$ids_to_insert = $data["ids"];
$row_to_insert = $mysqli->query("SELECT * FROM oczekujace WHERE ID='".$ids_to_insert[0]."'");
$inser_row = $mysqli->query("INSERT INTO zaakceptowane(nazwa) VALUES('".$row_to_insert['nazwa']."')");
}
?>
EDIT
Ok, i fixed this. Just deleted contentType...
$.ajax({
type: "POST",
url: "request.php",
//dataType : "json",
//contentType: "application/json; charset=utf-8",
data: {data : data_to_send},
success: function(response){
console.log(response);
console.log("Dodałem rekord!");
},
});
You can use json_decode() function. Its return php array which you can use to insert data in mysql. To read more about json_decode() visit this link.
http://php.net/manual/en/function.json-decode.php
In AJAX use dataType: 'json',
var data_to_send = {
event : "accept",
ids : ids_to_accept,
}
console.log(ids_to_accept);
$.ajax({
type: "POST",
url: "request.php",
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: data_to_send,
success: function(response){
console.log("Dodałem rekord!");
},
});
And in your PHP File use json_decode()
include_once("connect.php");
if($_POST){
$data = json_decode($_POST);
//manipulate $data
$ids_to_insert = array();
$ids_to_insert = $data["ids"];
$row_to_insert = $mysqli->query("SELECT * FROM oczekujace WHERE ID='".$ids_to_insert[0]."'");
$inser_row = $mysqli->query("INSERT INTO zaakceptowane(nazwa) VALUES('".$row_to_insert['nazwa']."')");
}
It will work as you desire.
$_POST['data'] is undefined, because its not present in data_to_send.
You have to either access the posted elements directly in $_POST:
$event = $_POST["event"]);
$ids_to_insert = $_POST["ids"];
or you can change the JS part and provide the data element:
$.ajax({
type: "POST",
url: "request.php",
data: {
"data": data_to_send,
},
success: function(response){
console.log("Dodałem rekord!");
}
});
no need for json_decode, jQuery already converts the data object into a urlencoded parameter string, which PHP in turn converts into an array.
I'm using this ajax to pass an array of strings to another page.
This is the array on nomes.php
[ 'Home','A empresa','funcionarios','Quem Somos?','Historia','Inicio',]
This is the code, the alert doesn't work - can anyone help?
$.ajax({
type: 'post',
url: 'nomes.php',
beforeSend: function(x) {
if(x && x.overrideMimeType) {
x.overrideMimeType("application/j-son;charset=UTF-8");
}
},
dataType: "json",
success: function(data){
v=data;
alert(v);
}
});
You need to encode the PHP-array to a JSON-array.
<?php
echo (json_encode($myPhpArray));
?>
You can serialize the array into JSON using stringify to pass them through. Add this to your Ajax call:
data: JSON.stringify(arr);
Replace arr with your JavaScript array. This needs a plugin though. Have a look at this answer for more info.
Alternatively, if your array is in PHP, not in JavaScript you can echo it out directly like this:
data: <?php echo json_encode($arr) ?>,
If you're trying to pass the json to the server, you can do it like :
var strArray = ['str1', 'str2', 'str3'];
$.ajax({
type: 'post',
url: 'nomes.php',
data: {strarray: strArray},
dataType: "json",
success: function(data){
v=data;
alert(v);
}
});
If you're trying to receive it, you need to set the header on php side :
header('content-type: application/json');
echo json_encode(array('str1', 'str2', 'str3'));
i want to pass array which is generated by javascript..just say i have an array named vals2=('john','peter') and i want to pass this array to my php page(insert_paket_f.php).
this is my ajax code :
$.ajax({
type: "POST",
url: "insert_paket_f.php",
data: { data : vals2 },
cache: false,
//vals=('john','peter','andrea');
success: function(){
alert("OK");
}
});
insert_paket_f.php
$data1 = $_POST['data'];
$data1 = explode(",", $_POST['data']);
print_r($data1);
when i run my browser, its show empty array, and just looks like this Array ( [0] => )
how can i fix this?
thanks..
Try this:
Use join to send the javascript array as an string.
Javascript
var vals2 = ['john','peter'];
$.ajax({
type: "POST",
url: "insert_paket_f.php",
data: { data : vals2.join(',') },
cache: false,
success: function(){
alert("OK");
}
});
PHP
$data1 = $_POST['data'];
$data1 = explode(",", $_POST['data']);
print_r($data1);
Hope it helps.
Try:
$data1 = json_decode($_POST['data']);
print_r($data1);
In the Data Feild try to use variables
as
echo data: data[]=john&data[]=peter&data[]=andrea
Code:-
$.ajax({
type: "POST",
url: "insert_paket_f.php",
data: "data[]=john&data[]=peter&data[]=andrea",
cache: false,
success: function(){
alert("OK");
}
});
Is It working?
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
send arrays of data from php to javascript
I know there are many questions like this but I find that none of them are that clear.
I have an Ajax request like so:
$.ajax({
type: "POST",
url: "parse_array.php",
data: "array=" + array,
dataType: "json",
success: function(data) {
alert(data.reply);
}
});
How would I send a JavaScript array to a php file and what would be the php that parses it into a php array look like?
I am using JSON.
Step 1
$.ajax({
type: "POST",
url: "parse_array.php",
data:{ array : JSON.stringify(array) },
dataType: "json",
success: function(data) {
alert(data.reply);
}
});
Step 2
You php file looks like this:
<?php
$array = json_decode($_POST['array']);
print_r($array); //for debugging purposes only
$response = array();
if(isset($array[$blah]))
$response['reply']="Success";
else
$response['reply']="Failure";
echo json_encode($response);
Step 3
The success function
success: function(data) {
console.log(data.reply);
alert(data.reply);
}
You can just pass a javascript object.
JS:
var array = [1,2,3];
$.ajax({
type: "POST",
url: "parse_array.php",
data: {"myarray": array, 'anotherarray': array2},
dataType: "json",
success: function(data) {
alert(data.reply); // displays '1' with PHP from below
}
});
On the php side you need to print JSON code:
PHP:
$array = $_POST['myarray'];
print '{"reply": 1}';
HI,
use json_encode() on parse_array.php
and retrive data with json_decode()