简体   繁体   中英

Pagination not working using php and ajax

Below mentioned are 3 different files 1st file Package.php code:

<body>
    <form action='Add_Package.php' method='POST'>
    <input type='submit' id='btnAdd' value='Add Package Details'>

    <select id="pack_type" name="pack_type" onChange="package_changed()" >
    <option value="1">Valid</option>
    <option value="0">In-Valid</option>
    </select>
    <center><h1>Package Details</h1></center>
    <div id="package_info" name="package_info"> </div>
    <script type="text/javascript" src="js/service.js" ></script>
</body>

2nd File service.js code:

function package_changed()
{
    var xmlhttp;
    var type;
    type=document.getElementById("pack_type").value;
    if(type=="label")
    return;
    if(window.XMLHttpRequest)
    {
    xmlhttp=new XMLHttpRequest();
    }
    else
    {           
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {                   
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {                       
            document.getElementById("package_info").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("GET","Package_description.php?type="+type,true);
    xmlhttp.send();         
}

3rd file Package_description.php code: $perpage=10;

    $row_count = mysql_query("select count(package_id) from package where is_valid=".$_GET['type']);
    $pages = ceil(mysql_result($row_count,0) / $perpage);   
    $page=(isset($_GET['page'])) ? (int)$_GET['page'] : 1;
    $start=($page - 1) * $perpage;

    $result = mysql_query("SELECT * FROM package where is_valid='".$_GET['type']."'ORDER BY package_id DESC LIMIT $start , $perpage ");

    echo '<div class="pagination pagination-middle pagination-right"><ul>';
    if($pages >= 1 && $page <=$pages)
    {
    for($x=1; $x<=$pages; $x++)
    {           
        if($x==$page)
    {
    echo '<li class="disabled"><a href="?page='.$x.'">'.$x.'</a></li>';
    }
    else
    {
    echo '<li class="active"><a href="?page='.$x.'">'.$x.'</a></li>';
    }                   
    }       
    }
    echo '</ul></div>';
    echo "  <center>                                
    <table class='table table-hover' border=5>
    <th>Package Name</th>
    <th>Package Credits</th>
    <th></th>";

    while($row=mysql_fetch_array($result))
    {
    echo "  <tr>                
    <td>" .$row['package_name'] ."</td>
    <td>" .$row['package_credits'] ."</td>
    <td><center><a href='Edit_Package.php?id=".$row['package_id'] . "'>Edit</a></center </td>
    </tr>";
    }
    echo "  </table><br>            
     </center>
     </form>";  
?>

Que: When I click on valid or invalid option in 1st file it will display the data properly. When the records are more than 10 then when I click on 2nd page It is not displaying the records from the 2nd page. The Problem is with javascript function because the same pagination script runs well when present on the same page here 3 different pages are involved including javascript function I don't know where exactly the problem is does any body have any solution please do let me know. Thanks in advance.

I actually found the solution for my problem Here are the changes what I did to my service.js file code:

function package_changed()
{
        var xmlhttp;
        var type;
        type=document.getElementById("pack_type").value;
        var url=document.URL;
        var thenum = url.replace( /^\D+/g, '');

        if(type=="label")
            return;
        if(window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {           
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {                   
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {                       
        document.getElementById("package_info").innerHTML=xmlhttp.responseText;
        }
        }
        if(thenum=="")
        {
        xmlhttp.open("GET","Package_description.php?type="+type+"&page=1",true);
        xmlhttp.send(); 
        }
        else
        {
        xmlhttp.open("GET","Package_description.php?type="+type+"&page="+thenum,true);
        xmlhttp.send();         
        }
}

Actually I was not able to capture page variable and pass it onto package_description.php file so to that I captured package.php file url and stored into url variable and then I extracted only number part of it using .replace() function and some little bit changes as per the need hence done. Hope this thing might help someone in the future when similar problem arise. Any query then do comment. Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM