【问题标题】:Pagination: how to move to next page by selecting submit分页:如何通过选择提交移动到下一页
【发布时间】:2015-12-28 19:25:22
【问题描述】:

我有一个名为 Quizes.php 的主页,其中包含一个显示页面列表的分页脚本,即 [1][2][3]..... 我的代码与包含链接的<a href ....> 一起工作正常到“下一个”、“上一个”、“最后一个”和“第一个”,但在 '<form method="get" action="?page=".$next."" >'; 提交按钮不起作用。看起来action="?page=".$next.""是错误的。那么最好的方法是什么?

我也遇到了这些链接 PHP: Pagination with submit buttonSubmit form in a displaytag pagination 和其他但没有帮助。

<?php

                $page = $_GET['page'];

                if($page == ""){

                    $page = "1";

                }else{

                    // If page is set, let's get it
                    $page = $_GET['page'];

                }

                // Now lets get all messages from your database
                $sql = "SELECT * FROM quizes";
                $query = mysql_query($sql);

                // Lets count all messages
                $num = mysql_num_rows($query);

                // Lets set how many messages we want to display
                $per_page = "2";

                // Now we must calculate the last page
                $last_page = ceil($num/$per_page);

                echo 's', $num;

                // And set the first page
                $first_page = "1";

                // Here we are making the "First page" link
                echo "<a href='?page=".$first_page."'>First page</a> ";

                // If page is 1 then remove link from "Previous" word
                if($page == $first_page){

                    echo "Previous ";

                }else{

                    if(!isset($page)){

                        echo "Previous ";

                    }else{

                        // But if page is set and it's not 1.. Lets add link to previous word to take us back by one page
                        $previous = $page-1;

                        echo "<a href='?page=".$previous."'>Previous</a> ";

                    }

                }

                // If the page is last page.. lets remove "Next" link
                if($page == $last_page){

                    echo "Next ";   

                }else{

                    // If page is not set or it is set and it's not the last page.. lets add link to this word so we can go to the next page
                    if(!isset($page)){

                        $next = $first_page+1;
                        echo "<a href='?page=".$next."'>Next</a> ";

                    }else{

                        $next = $page+1;
                        echo "<a href='?page=".$next."'>Next</a> ";

                    }

                }

                // And now lets add the "Last page" link
                echo "<a href='?page=".$last_page."'>Last page</a>";

                // Math.. It gets us the start number of message that will be displayed
                $start = ($page-1)*$per_page;

                // Now lets set the limit for our query
                $limit = "LIMIT $start, $per_page";

                // It's time for getting our messages
                $sql = "SELECT * FROM quizes $limit";

                $query = mysql_query($sql);

                echo "<br /><br />";

                // And lets display our messages

                $i=0;
                while($row = mysql_fetch_array($query) or die(mysql_error())){
                $a= $row['A'];
                echo '<form method="get" action="?page=".$next."" >'; // is that correct way?????
                    while ($row=mysql_fetch_array($query))
                    {

                    echo '<div class="boxed" >';

                    echo "\t".'<tr><th>'.
                    $row['question']."<br>".
                    '</th><th>'."<input type='radio' name= '.$i' value='{$row['A']}'>".$row['A']."<br>".
                    '</th><th>'."<input type='radio' name='.$i' value='{$row['B']}'>".$row['B']."<br>".
                    '</th><th>'."<input type='radio' name='.$i' value='{$row['C']}'>".$row['C']."<br>".
                    '</th><th>'."<input type='radio' name='.$i' value='{$row['D']}'>".$row['D'].'</th>
                    </tr>';
                //  echo '<input type="hidden" name="page" value="">';
                    echo '<input type="submit" name="submit"/>';
                    $i++;

            echo '</div>';

                        echo '</div>';
                    }
                echo '</form>';
                }



                  ?>

请帮帮我。

【问题讨论】:

    标签: php html pagination


    【解决方案1】:

    Get 方法在你想要的 url 中发送数据。您不需要在操作中发送数据,否则将输入字段命名为 page 并将其值设置为页码。这样你提交的时候,数据就会按照你想要的方式提交 用这个替换你的while循环

    while($row = mysql_fetch_array($query) or die(mysql_error())){
                $a= $row['A'];
                echo '<form method="get">'; 
                    while ($row=mysql_fetch_array($query))
                    {
    
                    echo '<div class="boxed" >';
    
                    echo "\t".'<tr><th>'.
                    $row['question']."<br>".
                    '</th><th>'."<input type='radio' name= '.$i' value='{$row['A']}'>".$row['A']."<br>".
                    '</th><th>'."<input type='radio' name='.$i' value='{$row['B']}'>".$row['B']."<br>".
                    '</th><th>'."<input type='radio' name='.$i' value='{$row['C']}'>".$row['C']."<br>".
                    '</th><th>'."<input type='radio' name='.$i' value='{$row['D']}'>".$row['D'].'</th>
                    </tr>';
                    echo '<input type="hidden" name="page" value="'.$next.'">';
                    echo '<input type="submit" name="submit"/>';
                    $i++;
    
            echo '</div>';
    
                        echo '</div>';
                    }
                echo '</form>';
                }

    答案已更新

    【讨论】:

    • 您可以跳过获取当前页面 url 以进行操作。为了安全起见,我只是添加了它。我认为没有它也可以工作。
    • 解析错误:语法错误,意外的 'http' (T_STRING),期待 ',' 或 ';'
    • 在提交之前取消注释隐藏字段并从表单中删除操作。用此行替换提交前的评论。 echo '';
    • 你能重新检查你的代码吗,因为我是这样的:tinypic.com/r/10dtik9/9
    • 您是否添加了第二个代码而没有添加我的第一个代码?不要添加我的第一个代码。做第二个
    【解决方案2】:

    看起来像是语法错误。

    试试:

    echo '<form method="get" action="?page='.$next.'">';
    

    .$next. 周围加上单引号,因为您在echo 中使用了单引号。如果使用双引号,它会关闭 action 属性(解释为 javascript,而不是 PHP)。

    另外,您可以查看您的 PHP 错误日志。

    【讨论】:

    • PHP 错误日志中有内容?我确定语法错误,但可能还有另一个问题。
    • 如果您无法访问 PHP 服务器上的 PHP 日志,请将此行放在 PHP 脚本的开头:error_reporting(E_ALL);
    • 这里是我的代码的完整文件。我现在很困惑:uploadmb.com/dw.php?id=1451295778
    • 我无法正确测试它,它会产生很多错误,因为我没有数据库和其他页面。我猜你脚本的结尾部分有错误。你说“提交”按钮不起作用,但是当你点击它时会发生什么?
    • 当我点击相同的页面刷新
    【解决方案3】:
     <?php
     //before you begin - try to know how get works
     //clean your code a bit, by mentioning form outside of while loop
     //you almost get everything in print_r($_GET); after submit
     //i think its better to attach data you get(value from radios,textfields) with that of the current page value(page=1)
     //page - a tag - page = 1
     //$string = "option1=a";
     //$page = 1;
     //finalstring = $string.'&'.page=$page;
     //url - To next a tags http://example.com/$finalstring
     ?>
     <form action='Quizes.php' method='GET'>
     <?php
          $i = 0;
          //test case
          while($i <= 5){
               echo '<div class="boxed">';     
               echo "<input type='radio' name= 'ans[]' value='$i' $checked > $i";
               echo '</div>';
               $i++;
          }
    
     ?>
     <input type='submit' name='submit' value='submit'/>
     </form>
    

    【讨论】:

    • 谢谢。你能解释一下我应该用我现有的“echo '
      ';”替换这段代码吗??
    • bcz 我很困惑是代码中的替换还是添加?
    • 试试这个并与你的比较runnable.com/U8dzQWEzMxxqeQ_E/…
    • 我已经看到了,但我的逻辑不同,我只是卡在提交 bcz 页面没有转移到下一个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多