简体   繁体   中英

Pagination: how to move to next page by selecting submit

I have a main page called Quizes.php that contain a pagination script which displays a list of pages ie [1][2][3]..... My code is working fine with <a href ....> which contain link to "Next","Previous" "last" and "First" but in the '<form method="get" action="?page=".$next."" >'; submit button is not working. It looks like action="?page=".$next."" is wrong. So whats the best way to do so?

I also meet these links PHP: Pagination with submit button , Submit form in a displaytag pagination and other but no help.

<?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>';
                }



                  ?>

Kindly help my way.

Get method sends the data in the url which you want. You don't need to send data in action else make input field named page and set its value to page number. In this way when you submit , data will be submitted in the way you want Replace your while loop with this

 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>'; } 

Answer updated

It looks like a syntax error.

Try:

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

with single quotes around .$next. because you used single quotes with echo . If you use double quotes, it closes the action attribute (interpreted as javascript, not PHP).

Also, you can look in your PHP error log.

 <?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>

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