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 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么
  • ¥15 网站显示不安全连接问题
  • ¥15 github训练的模型参数无法下载
  • ¥15 51单片机显示器问题
  • ¥20 关于#qt#的问题:Qt代码的移植问题
  • ¥50 求图像处理的matlab方案
  • ¥50 winform中使用edge的Kiosk模式
  • ¥15 关于#python#的问题:功能监听网页