PHP and AJAX PAGER

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mhonnphp
Forum Commoner
Posts: 37
Joined: Fri Oct 12, 2007 11:29 pm
Location: Philippines
Contact:

PHP and AJAX PAGER

Post by mhonnphp »

Guys,

I need help with PHP and Ajax Pager.
Please post your codes or Links for tutorials.
Thanks


Raymonn
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

Re: PHP and AJAX PAGER

Post by Glycerine »

This is a function I use all the time. Its not Asynchronous but it call a php function with reload. Works great -

for this example it calls a script and adds an email to a database for a mailing list...
I wont write the PHP code, I'm assuming you know that bit - if not - Ask, and I'll post it.

Your main page - example.html

Code: Select all

 
<div id='div_example'>
<span id="span_prompt">enter your email:</span>
<input type='text' name='TXT_mailing_list_email' id='TXT_mailing_list_email'>
<input type="button" value="Sign Me Up" onclick="javascript&#058; mailing_list_submit()" name="BTNsignmeup" id="BTNsignmeup">
</div>
 
Your Javascript code - js_ex.js

Code: Select all

 
function mailing_list_submit(){
    var email = document.getElementById("TXT_mailing_list_email"); 
    var txtp = document.getElementById("TXT_mailing_list_email_Prompt");
//personal function made to do a call and return it to the function noted in the last Variable -
// Var 1 - "/mailing_list/do.php?&email=' + email - to your PHP page - this also passes a variable via GET/POST method.
// Var 2 - "NONE" -  The container to which you want your results to go. This is handled by the final function - so, you can pretty much pass anything you want.
// Var 3 - "mailing_list_return" - the function called when a response is made...
    DoAjaxCall("/mailing_list/do.php?&email=' + email, "NONE", "mailing_list_return");
}
 
//this function is the return from the function above. Alerted so you can see it happen.
function mailing_list_return(Return_Info, Container){
    alert(unescape(Return_Info));
var Return_Div = document.getElementById("div_example"); 
var Return_Text_Container = document.getElementById("span_prompt"); 
 
//Do what you will with the return info, for example here, send it back to the input.
if(Return_Info == "GOOD"){
Return_Div.innerHTML = "Great, Thank you for signing up";
}else if(Return_Info == "NO EMAIL"){
Return_Text_Container.innerHTML = "This email is not correct";
}//etc error checking on return blah...
 
 
 
 
 
 
//just copy paste the below - --------------------------------------------------------
 
function DoAjaxCall(Page_name, container, Function_return){
    //start loading status
    //call ajax - Function to send to...
    
    url = Page_name + "";
    AjaxReg( url, Function_return, container)
}
 
 
function AjaxReg(url, Function_return, Container_Element_NAMEONLY) {
 
    var page_request = false
    if (window.XMLHttpRequest) // if Mozilla, Safari etc
        spage_request = new XMLHttpRequest()
    else if (window.ActiveXObject){ // if IE
        try {
            spage_request = new ActiveXObject("Msxml2.XMLHTTP")
        } 
        catch (e){
            try{
                spage_request = new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch (e){}
        }
    }
    else
        return false
    
        spage_request.open('GET', url, false) //get page synchronously 
        spage_request.send(null)
        RegAjaxWrite(spage_request, Function_return, Container_Element_NAMEONLY);
}
 
 
 
function RegAjaxWrite(page_request, Function_return, iContainer_Element_NAMEONLY){
 
    var iContainer_Element_NAMEONLY;
    
    if (window.location.href.indexOf("http")==-1 || page_request.status==200)
    //wt should be the function to passto which is Return_[Step_No](Respose)
    eval(Function_return + "('" + escape(page_request.responseText) + "', '"+ iContainer_Element_NAMEONLY + "')")
    //document.getElementById(iContainer_Element_NAMEONLY).innerHTML = "SEND";
}
// -------------------------------------------------
 
You PHP Page "/mailing_list/do.php"

Code: Select all

 
$email = $_GET['email']; //should perform Mysql safe stripping functions here!
 
/* This is where you PHP code is... 
error check the email...
connect to the database, perform you bits...
*/
$return_from_database = true; //this is just an example of stating the mysql query you performed was successful
 
//these echo's are returned to your javascript function.. send anything like specific results... 
if($return_from_database == true){
echo "GOOD";
}else{
echo "BAD";
}
 

That seems to be it. I know the examples clunky. What you really need is the Ajax call functions and you can pretty much Pimp it out for everything - I use that function every day and as its so simple to use, its implemented in seconds.

If you need a more indepth example Just ask.

Hope it helps.
mhonnphp
Forum Commoner
Posts: 37
Joined: Fri Oct 12, 2007 11:29 pm
Location: Philippines
Contact:

Re: PHP and AJAX PAGER

Post by mhonnphp »

Glycerine,

Thanks...
I really appreciate your help
but what I need is -
PAGER using Ajax and php.. or PAGING..
My bad! Sorry I didn't explain it well..

--------------------------------------

Anyone has pager or paging using Ajax and Php??
Please help!Thanks

Raymonn..
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP and AJAX PAGER

Post by califdon »

mhonnphp wrote:Anyone has pager or paging using Ajax and Php??
Please help!Thanks

Raymonn..
Google search produced:
http://satya61229.blogspot.com/2007/03/ ... -ajax.html
http://www.nitobi.com/kb/?artid=283
http://www.ajax-tutorials.com/tutorial- ... on_script/
mhonnphp
Forum Commoner
Posts: 37
Joined: Fri Oct 12, 2007 11:29 pm
Location: Philippines
Contact:

Re: PHP and AJAX PAGER

Post by mhonnphp »

Thanks
Glycerine
Forum Commoner
Posts: 39
Joined: Wed May 07, 2008 2:11 pm

Re: PHP and AJAX PAGER

Post by Glycerine »

:D Thats the oddest none...none typo I've come across... Erm.... Nevermind eh!
mhonnphp
Forum Commoner
Posts: 37
Joined: Fri Oct 12, 2007 11:29 pm
Location: Philippines
Contact:

Re: PHP and AJAX PAGER

Post by mhonnphp »

Heres the paging code..
(suggestions, revisions, comments or any violent reactions are welcome as long as it improves this code).

HTML

Code: Select all

 
 <html>....
   <div id='Target_Div_id>
   </div>
 </html>
 

AJAX(external js)

Code: Select all

 
 var NameOfFunction_xmlHttp
  
 //CALL THIS FUNCTION USING ONCLICK OR ETC.. 
 //THIS FUNCTION USE TO PASS THE DATA / VALUES TO YOUR PAGING FILE(PHP)                               
 function NameOfFunction_showData(cID,sType,sValue,pNum)
 {
    if (cID.length==0)
    {                                       
                                             //NAME OF YOUR DIV
        document.getElementById("Target_Div_id").innerHTML=""
        return
    }   
    NameOfFunction_xmlHttp=NameOfFunction_GetXmlHttpObject()
    if (NameOfFunction_xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    } 
 
    var url="ajax_request/PHPFile.php"
 
    url=url+"?country="+cID
    url=url+"&Type="+sType
    url=url+"&Value="+sValue    
    url=url+"&page="+pNum       
    url=url+"&sid="+Math.random()
 
    NameOfFunction_xmlHttp.onreadystatechange=NameOfFunction_stateChanged 
    NameOfFunction_xmlHttp.open("GET",url,true)
    NameOfFunction_xmlHttp.send(null)
} 
 
 
function NameOfFunction_stateChanged() 
{ 
    if (NameOfFunction_xmlHttp.readyState==4 || NameOfFunction_xmlHttp.readyState=="complete")
    {                                   
                                             //NAME OF YOUR DIV
        document.getElementById("Target_Div_id").innerHTML=NameOfFunction_xmlHttp.responseText 
    } 
}
 
function NameOfFunction_GetXmlHttpObject()
{
    var NameOfFunction_xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        NameOfFunction_xmlHttp=new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            NameOfFunction_xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            NameOfFunction_xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
 
    return NameOfFunction_xmlHttp; 
 }
 
PHPCode(or any paging code)

Code: Select all

 
<html>
   <head>
       <title>Paging Using PHP</title>
   </head>
 
   <body>
        <?php
            ######################################
            #   GET VALUE PASS BY YOUR AJAX FILE
            $country_ID = $_GET['country'];         
            $search_type = $_GET['Type'];                       
            $search_value = $_GET['Value'];                     
            ###############################
        
            
            ####################
            #   Get CONFIG
            require('../../Connections/config2.php');
 
            $rec_limit = 2;
            
            $conn = mysql_connect($host, $user, $pass);
            if(! $conn )
            {
              die('Could not connect: ' . mysql_error());
            }
            mysql_select_db($db);
            
            #####################################
            /* Get total number of records */
            $sql = "SELECT count(art_ID) FROM ivc_articles WHERE c_id = ".$country_ID." ".$andSearch." ORDER BY art_postDate DESC";
            ############################################
            
            
            $retval = mysql_query( $sql, $conn );
            if(!$retval )
            {
              die('Could not get data: ' . mysql_error());
            }
            $row = mysql_fetch_array($retval, MYSQL_NUM );
            $rec_count = $row[0];
            
            if( isset($_GET{'page'} ) )
            {
                $page = $_GET{'page'} + 1;
                $offset = $rec_limit * $page ;
            }
            else
            {
                $page = 0;
                $offset = 0;
            }
            $left_rec = $rec_count - ($page * $rec_limit);
            
            $sql = "SELECT * ".
                     "FROM ivc_articles WHERE c_id = ".$country_ID." ".$andSearch."ORDER BY art_postDate DESC ".
                     "LIMIT $offset, $rec_limit";
            
            $retval = mysql_query( $sql, $conn );
            if(! $retval )
            {
              die('Could not get data: ' . mysql_error());
            }
            
            
            echo "<h5>Articles & Visa Updates</h5>(Newly Posted)";
            echo "<h4>".$CountryName."</h4><br />";         
            ###########################################################
            #   Query show data
            while($rows = mysql_fetch_array($retval, MYSQL_NUM))
            {
            echo $rows[3]."<br />";     //display names
            echo "<b>".$rows[1]."</b> ";        //display names
            echo "<br /><a href='#' id='".$rows[0]."' onclick='artContent_showData(this.id); return false;'>read<img src='../Images/IVC/more.jpg' height='12px' border='0'></a><br />";     //display names                             
                echo "<br />";                          
            } 
            ########################################################
            
            mysql_close($conn);
            
            ###################################################
            #   Controll the Navigator Page
 
            $i = $rec_count;
            $num = 1;
            while($i > 0)
            {
                $pageNum = $num - 2;
                echo "<a href='#' onClick ="."page_showData(".$country_ID.",".$search_type.",'".$search_value."',".$pageNum."); return false;'>".$num."</a> ";
                $num = $num + 1;            
                $i= $i - $rec_limit;
                
            }
    
            
            
      ?>
    </body>
</html>     
 
 

sorry I know my codes are not good enough, thats why I post it here and open for any (suggestions, revisions, comments or any violent reactions are welcome as long as it improves this code).

anyway the logic.. just pass all the variables needed by your paging phpfile using your ajax. and target the div where the output of your php shows.
Post Reply