duanpei4455 2013-05-10 19:18
浏览 200
已采纳

分页不使用php和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.

  • 写回答

1条回答 默认 最新

  • dqd72925 2013-05-11 05:03
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件