Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
QUESTION Load ajax subpage
#1
Hi. This code echo all subpage. How show only 10 subpage and another load after with ajax.
PHP Code:
function getSubMenu($page){ 

 
$children=getChildren($page); 

 foreach (
$children as $subpage){ 
    
$title=returnPageField($subpage,'title'); 
    
$url=returnPageField($subpage,'slug'); 
    echo 
'<a href="/'.$url.'.html"> '.$title.'</a>'
  }


How make php function which show subpage (number from ajax). For example ajax:
Code:
$.ajax({  
                    type: "POST",  
                    url: "",
                    context: this,
                    data: {
                              'num'       : '10'
                           },
                    success: function(data){
                      console.log(data);
                    },
                    error: function(data){
                      console.log('error' + data);
                    }
                });
Reply
#2
Slice the array using the page number via query string
See array_slice
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#3
(2014-07-19, 13:08:57)shawn_a Wrote: Slice the array using the page number via query string
See array_slice
Thanks, i add this code in functions.php

PHP Code:
if($_POST['ajaxload']) {

    
$num $_POST['ajaxload'];    
    
$children=getChildren(news);
    
$sliced array_slice($children0$num);
    
    foreach (
$sliced as $subpage){
        
$title=returnPageField($subpage,'title');
        
$url=returnPageField($subpage,'slug');
        echo 
'<li><a href="/'.$url.'.html">'.$title.'</a></li>';
     }


Code:
$('body').on('click', '#load', function() {

    
    $.ajax({  
                    type: "POST",  
                    url: "/theme/news/functions.php",
                    context: this,
                    data: {
                           ajaxload : '10'
                    },
                    success: function(data) {
             console.log(data);
                        alert(data);
                    },
                    error: function(output){
                      alert(resp);
                    }
                });
        
});

But onclick error

Quote:<b>Fatal error</b>: Call to undefined function getChildren()
Confused
Reply
#4
are you tying to run this outside of GS ?
pretty sure thats is indeed a function in core, you are trying to load it directly outside of GS just submit to index not functions.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#5
(2014-07-20, 00:30:50)shawn_a Wrote: are you tying to run this outside of GS ?
pretty sure thats is indeed a function in core, you are trying to load it directly outside of GS just submit to index not functions.
This php code is in functions.php my theme of GetSimple.

Simple code work

PHP Code:
if($_POST['ajaxload']) {

    
$num $_POST['ajaxload'];    
    
$newnum $num*2;
        echo 
''.$newnum.'';


return 20.

But when i try
PHP Code:
if($_POST['ajaxload']) {

    
$num $_POST['ajaxload'];    
    
$children=getChildren(news);
    
$sliced array_slice($children0$num);
    
    foreach (
$sliced as $subpage){
        
$title=returnPageField($subpage,'title');
        
$url=returnPageField($subpage,'slug');
        echo 
'<li><a href="/'.$url.'.html">'.$title.'</a></li>';
    }



return [object Object] and POST /functions.php 500 (Internal Server Error)
Reply
#6
you do not send forms to your functions you send them to your index.
functions is loaded in global scope automatically and your code will run.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#7
You also want to wrap it in a function and call in your template somehow if you want to use core functions
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#8
(2014-07-20, 01:58:29)shawn_a Wrote: you do not send forms to your functions you send them to your index.
functions is loaded in global scope automatically and your code will run.
(2014-07-20, 02:39:42)shawn_a Wrote: You also want to wrap it in a function and call in your template somehow if you want to use core functions

I have bad english and knowledge of php Smile, I still don't understand why it doesn't work.

Could you write a working example code?
Reply
#9
I make file ajax.php in my theme, global functions will be work when ajax call it? How i can do it work?
Reply




Users browsing this thread: 1 Guest(s)