Posts: 5
Threads: 1
Joined: Jul 2014
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);
}
});
Posts: 6,266
Threads: 181
Joined: Sep 2011
Slice the array using the page number via query string
See array_slice
Posts: 5
Threads: 1
Joined: Jul 2014
(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($children, 0, $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()
Posts: 6,266
Threads: 181
Joined: Sep 2011
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.
Posts: 5
Threads: 1
Joined: Jul 2014
2014-07-20, 01:00:10
(This post was last modified: 2014-07-20, 01:21:38 by werty1001.)
(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($children, 0, $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)
Posts: 6,266
Threads: 181
Joined: Sep 2011
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.
Posts: 6,266
Threads: 181
Joined: Sep 2011
You also want to wrap it in a function and call in your template somehow if you want to use core functions
Posts: 5
Threads: 1
Joined: Jul 2014
(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
, I still don't understand why it doesn't work.
Could you write a working example code?
Posts: 5
Threads: 1
Joined: Jul 2014
I make file ajax.php in my theme, global functions will be work when ajax call it? How i can do it work?