I'm trying to connect the bootstrap treeview but the json script is not working. here is the fetch.php source code: `
$sub_data["id"] = $row["id"];
$sub_data["invited"] = $row["invited"];
$sub_data["text"] = $row["invited"];
$sub_data["caller_id"] = $row["caller_id"];
$data[] = $sub_data;
foreach ($data as $key => &$value)
{
$output[$value["id"]] = &$value;
}
foreach ($data as $key => &$value)
{
if ($value["caller_id"] && isset($output[$value["caller_id"]]))
{
$output[$value["caller_id"]]["nodes"][] = &$value;
}
}
foreach ($data as $key => &$value)
{
if ($value["caller_id"] && isset($output[$value["caller_id"]]))
{
unset($data[$key]);
}
}
echo json_encode($data);`
and this is the treeshow.php script code:
$(document).ready(function(){
$.ajax({
url: "fetch.php",
method:"POST",
dataType: "json",
success: function(data)
{
$('#treeview').treeview({data: data});
}
});
});
and mysql db fields: id, invited, caller_id what is wrong with this codes?
Related
I am dynamically adding table rows by selecting options from dropdown and then I am trying to send html table rows to php function using ajax as array in json format. However php function does not print all rows in the console log, when I am submitting more than one row. I got the desired output like once or twice. I think I am missing something in the php function. Please check once. If anymore information about the code is required, let me know, I will update.
Javascript:
function storeClgValues() {
var CollegeData= new Array();
$('#collegetable tr').each(function(row, tr){
CollegeData[row]={
"college" : $(tr).find('td:eq(0)').text()
}
});
CollegeData.shift();
return CollegeData;
}
$('#submit').click(function() {
CollegeData=storeClgValues();
CollegeData=$.toJSON(CollegeData);
$.ajax({
type:"POST",
url: '<?php echo base_url();?>ajaxController/insertcollege',
data: "CollegeData=" + CollegeData,
success: function(msg) {
console.log(CollegeData);
console.log(msg);
}
});
});
PHP function in AjaxController class:
public function insertcollege()
{
$data=array();
$data=stripcslashes($_POST['CollegeData']);
$data=json_decode($data, TRUE);
//echo $data[0]['college'].'<br>';
//echo $data[1]['college'].'<br>';
if (is_array($data) || is_object($data))
{
foreach ($data as $key => $item) {
echo $item['college'].'<br>';
}
}
}
Output in console in three tries:
[{"college":"College of Agriculture"}]
College of Agriculture
[{"college":"College of Agriculture"},{"college":"College of Business"}]
College of Agriculture
College of Business
[{"college":"College of Agriculture"},{"college":"College of Business"}, {"college":"College of Comm & Educati"}]
<!--nothing gets printed-->
Try like this...
<?php
$json = '[{"college":"College of Agriculture"},{"college":"College of Business"}, {"college":"College of Comm & Educati"}]';
$data = json_decode($json,TRUE);
//print_r($data);
if (is_array($data) || is_object($data))
{
foreach ($data as $key => $item) {
$output[]=$item['college'].'<br>';
}
}
echo json_encode($output);
?>
OR
<?php
$json = '[{"college":"College of Agriculture"},{"college":"College of Business"}, {"college":"College of Comm & Educati"}]';
$data = json_decode($json,TRUE);
//print_r($data);
if (is_array($data) || is_object($data))
{
foreach ($data as $key => $item) {
foreach($item as $value){
$output[] = $value."</br>";
}
}
}
echo json_encode($output);
?>
Please can someone tell me where I'm going wrong
js file:
//Checkboxid is an array which is populated using .push();
$.ajax({
type: "POST",
url: "test.php",
dataType: 'html',
data: { data: Checkboxid },
success: function (data) {
console.log(data);
}
});
test.php:
<?php
$test = $_POST['data'];
for ($i = 0; $i < count($test); ++$i) {
echo $test[$i];
}
foreach($test as $val) {
echo $val;
}
?>
console.log displays:
ArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArrayArray
Your $test variable is a two-dimensional array. Try this
<?php
$test = $_POST['data'];
foreach($test as $arr) {
foreach($arr as $val){
echo $val;
}
}
?>
I'm having a script which is generating a JSON array, based values user have selected. These values are sent as a JSON with AJAX to a PHP script which should receive this values and process it.
What could be wrong?
JSON (That is sent):
[{
"Pages":
{"name":" Page Name 1",
"id":"252456436636644"}
},
{
"Pages":{
"name":" Page Name 2",
"id":"345345435435232"
}
}]
Jquery:
var json_pages = JSON.stringify(publish);
$.ajax({
url: "post.php",
type: "post",
data: { PublishToPages: json_pages },
success: function(){},
error: function(){}
});
Problem is that the JSON I recieve from PHP isn't getting the data,
if($_POST['PublishToPages']) {
$json = $_POST['PublishToPages'];
$array = json_decode($json, true);
foreach($array as $item) {
$page_id = $item['Pages']['id'];
echo $page_id;
}
}
If I manually put in the JSON in the PHP script like this it Works,
if ($_POST['PublishToPages']) {
$json = '[{"Pages":{"name":" Page Name","id":"234545355345435"}},{"Pages":{"name":" Page Name 2","id":"345345435435435435"}}]';
$array = json_decode($json, true);
foreach($array as $item) {
$page_id = $item['Pages']['id'];
echo $page_id;
}
}
Try using this:
if($_POST['PublishToPages']) {
$json = $_POST['PublishToPages'];
$items = array();
$array = json_decode($json, true);
foreach($array as $item) {
$page_id = $item['Pages']['id'];
$items[] = $page_id;
}
echo json_encode($items);
}
Try this
$.ajax({
url: "post.php",
type: "post",
dataType:"json",
data: { PublishToPages: json_pages },
success: function(){},
error: function(){}
});
Thanks for all input! I figured it out using the var_dump and realized it was encoding error so added stripslashes(); and it worked! :)
if ($_POST['PublishToPages']) {
$json = stripslashes($_POST['PublishToPages']);
$array = json_decode($json, true);
foreach($array as $item) {
$page_id = $item['Pages']['id'];
echo $page_id;
}
}
how to retrieve much data in view (codeigniter) using while(). if I use foreach, I can't get the desired result. this is my code:
//my view home.php
$(document).ready(function(){
$('#check').click(function(event){
event.preventDefault();
var form_data = {
name: "xxxx"
};
$.ajax({
url: 'http://localhost:8000/jqjx/index.php/cont/getname',
type: 'POST',
async : false,
data: form_data,
dataType: 'html',
success: function(resp){
$('#content').html(resp);
}
});
return false;
});
});
//my controller cont.php
public function getname()
{
$data = array();
$namex = $this->input->post('name');
if($q = $this->my_model->detail_data($namex))
{
$data['data_detail'] = $q;
$this->load->view('tamp_page', $data);
}
}
my helper page (view) tamp_page.php
<?php
if(isset($data_detail))
{
foreach ($data_detailas $row) {
echo $row['name']."<br/>";
echo $row['birthday']."<br/>";
}
}
?>
if I use :
<?php
if(isset($data_detail))
{
echo $name_data['id_transactions'] . "<br/>";
echo $name_data['goods'] . "<br/>";
}
?>
it's still work but just for 1 data result. so how I can loop more much data.. thanks..
<?php
if(isset($data_detail))
{
foreach ($data_detail as $row)
{
echo $row['name']."<br/>"; //$row['name] was not properly closed. single quote(') was missing
echo $row['birthday']."<br/>";
}
}
?>
Just basic use of foreach:
if(isset($data_detail)){
foreach ($data_detailas as $row) {
echo $row['name'] . "<br/>";
echo $row['birthday'] . "<br/>";
}
}
Im using Zend Framework ..in one of my phtml file's i have this code
<script>
function foobar(id,type){
var idarray = <?php AppNamespace_General::getparentids( ?>id, type<?php ) ?>; // here the id and type are from js
//the php function returns a json array to the js variable
......
location.href = baseurl +'/somepage/id/'+id;
}
How can i correctly pass the js elements to the php function
The php function(Already thought of doing it via ajax..its quite complex)
public static function getparentids($id, $type, $elmarray = '') {
if (empty($elmarray)) { //avoiding redeclaration of array
$elmarray = array();
}
switch (strtolower($type)) {
case 'group':
case 'product':
case 'specification':
$gp_handler = new PackAssist_Model_DbTable_Groups();
$q = "SELECT * FROM t_groups WHERE group_id = $id";
$sql = $gp_handler->getAdapter()->query($q);
break;
case 'part':
$pt_handler = new PackAssist_Model_DbTable_Parts();
$q = "SELECT * FROM t_parts WHERE part_id = $id";
$sql = $pt_handler->getAdapter()->query($q);
break;
}
$result = $sql->fetchAll();
$i = 0;
if (count($result) > 0) {
foreach ($result as $row) {
if (isset($row['group_parent_id']) && $row['group_parent_id'] != 0) {
if (in_array($row['group_id'], $elmarray)) {
$e = $row['group_parent_id'];
} else if ($row['group_parent_id'] != 0) {
$e = $row['group_id'];
}
} else if (isset($row['part_group_id'])) {
$e = $row['part_group_id'];
} else if ($row['group_parent_id'] == 0) {
break;
}
if (isset($e) && !empty($e)) {
array_push($elmarray, $e);
}
self::getparentids($e, 'group', $elmarray);
$i++;
}
} else {
array_push($elmarray, $id);
}
array_pop($elmarray); //removing the group of super parent group which we dont need
if ($i == 0) { // just encode the array only once
echo json_encode(array_reverse($elmarray));
}
}
If you use jQuery, you can do the following to execute the JSON request:
$.ajax({
type: 'GET',
url: '/path/to/script.php',
data: '{ id: '+id+', type: '+type+' }',
contentType: 'application/json',
dataType: 'json',
success: function(data) {
dataObject = JSON.parse(data);
// process data
},
error: function(e) {
console.log(e.message);
}
});
You can use your existing PHP code with this solution. The url you point to would just have to print the JSON result, as you are currently doing in getparentids().