Это настоящий код, который я пишу, первая публикация содержит ту же проблему,

Как вы можете видеть, кнопки формы 1 предназначены для управления итерацией значения $ _SESSION ['clicks'], я использую сеансы в этой форме,

Кнопки формы 2 должны выводить массив для соответствующих имен, которые извлекаются из другого массива.

И проблема в том, что каждый раз, когда кнопки в form2 нажимаются или вызываются в php, другая форма или form1 тоже отправляются

     <?php 
        session_start();
        $results_per_page = 10;
        if (!isset($_GET['page'])) {
            $page = 1;
        }else{
            $page = $_GET['page'];
        }
        $stmt1 = $conn->prepare("SELECT*FROM doctors"); 
        $stmt1->execute();
        $stmt2 = $conn->prepare("SELECT*FROM doctors ORDER BY doctor_name ASC");
        $stmt2->execute();

        while($row = $stmt2->fetch()) {
            $result[] = [
                'name' => $row['doctor_name'],
                'degree' => $row['doctor_degree'],
                'foe' => $row['doctor_foe'],
            ];
        }
    ?>

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <div>
            <form method="post">
                <?php
                    $next = "<button class='btn-nav' name='plus'>Next</button>";
                    $prev = "<button class='btn-nav' name='min'>Prev</button>";

                    $next_un = "<button class='btn-nav' name='plus' disabled>Next</button>";
                    $prev_un = "<button class='btn-nav' name='min' disabled>Prev</button>";
                    $results = count($result);
                    $num_of_pages = ceil($results/$results_per_page);

                    $clicks = 0;
                    if (isset($_POST['plus'])) {
                        $_SESSION['clicks'] += 10 ;
                    }else {
                        $_SESSION['clicks'] -= 10;
                    }

                    if ($_SESSION['clicks']>0) {
                        echo $prev;
                    }else{
                        echo $prev_un;
                    }
                    if ($_SESSION['clicks']>=($results-10)) {
                        echo $next_un;
                    }else{
                        echo $next;
                    }

                    $newarr = array_slice($result, $_SESSION['clicks'], 10);
                ?>
            </form>
        </div>
        <div>
            <table class="appt">
                <?php 
                    foreach ($newarr as $row) {
                    echo "<tr><td>".$row['name']
                        ."</td><td>".$row['degree']
                        ."</td><td>".$row['foe']."</td></tr>";
                    }

                    echo $new_count = count($newarr);
                 ?>
            </table>
            <table class="appt">
                <form method="post">
                    <tr><th>Set Appointment</th></tr>
                    <?php
                        switch ($new_count) {
                            case '10':
                                echo '<tr><td><button type="button" name="sa1">Set Appointment</button></td></tr>';
                                echo '<tr><td><button type="button" name="sa2">Set Appointment</button></td></tr>';
                                echo '<tr><td><button type="button" name="sa3">Set Appointment</button></td></tr>';
                                echo '<tr><td><button type="button" name="sa4">Set Appointment</button></td></tr>';
                                echo '<tr><td><button type="button" name="sa5">Set Appointment</button></td></tr>';
                                echo '<tr><td><button type="button" name="sa6">Set Appointment</button></td></tr>';
                                echo '<tr><td><button type="button" name="sa7">Set Appointment</button></td></tr>';
                                echo '<tr><td><button type="button" name="sa8">Set Appointment</button></td></tr>';
                                echo '<tr><td><button type="button" name="sa9">Set Appointment</button></td></tr>';
                                echo '<tr><td><button type="button" name="sa10">Set Appointment</button></td></tr>';
                                break;
                            case '9':
                                echo '<tr><td><button name="sa1">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa2">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa3">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa4">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa5">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa6">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa7">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa8">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa9">Set Appointment</button></td></tr>';
                                break;
                            case '8':
                                echo '<tr><td><button name="sa1">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa2">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa3">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa4">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa5">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa6">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa7">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa8">Set Appointment</button></td></tr>';
                                break;
                            case '7':
                                echo '<tr><td><button name="sa1">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa2">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa3">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa4">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa5">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa6">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa7">Set Appointment</button></td></tr>';
                                break;
                            case '6':
                                echo '<tr><td><button name="sa1">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa2">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa3">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa4">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa5">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa6">Set Appointment</button></td></tr>';
                                break;
                            case '5':
                                echo '<tr><td><button name="sa1">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa2">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa3">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa4">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa5">Set Appointment</button></td></tr>';
                                break;
                            case '4':
                                echo '<tr><td><button name="sa1">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa2">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa3">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa4">Set Appointment</button></td></tr>';
                                break;
                            case '3':
                                echo '<tr><td><button name="sa1">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa2">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa3">Set Appointment</button></td></tr>';
                                break;
                            case '2':
                                echo '<tr><td><button name="sa1">Set Appointment</button></td></tr>';
                                echo '<tr><td><button name="sa2">Set Appointment</button></td></tr>';
                                break;
                            default:
                                echo '<tr><td><button name="sa1">Set Appointment</button></td></tr>';
                                break;
                        }
                    ?>
                </form>
            </table>
        </div>
        <?php 
            $sa1 = $_POST['sa1'] ?? '';
            $sa2 = $_POST['sa2'] ?? '';
            $sa3 = $_POST['sa3'] ?? '';
            $sa4 = $_POST['sa4'] ?? '';
            $sa5 = $_POST['sa5'] ?? '';
            $sa6 = $_POST['sa6'] ?? '';
            $sa7 = $_POST['sa7'] ?? '';
            $sa8 = $_POST['sa8'] ?? '';
            $sa9 = $_POST['sa9'] ?? '';
            $sa10 = $_POST['sa10'] ?? '';


            if (isset($_POST['sa1'])) {
                echo $newarr[0]['name'];
                echo $newarr[0]['degree'];
                echo $newarr[0]['foe'];
            }else if (isset($_POST['sa2'])) {
                echo $newarr[1]['name'];
                echo $newarr[1]['degree'];
                echo $newarr[1]['foe'];
            }else if (isset($_POST['sa3'])) {
                echo $newarr[2]['name'];
                echo $newarr[2]['degree'];
                echo $newarr[2]['foe'];
            }else if (isset($_POST['sa4'])) {
                echo $newarr[3]['name'];
                echo $newarr[3]['degree'];
                echo $newarr[3]['foe'];
            }else if (isset($_POST['sa5'])) {
                echo $newarr[4]['name'];
                echo $newarr[4]['degree'];
                echo $newarr[4]['foe'];
            }else if (isset($_POST['sa6'])) {
                echo $newarr[5]['name'];
                echo $newarr[5]['degree'];
                echo $newarr[5]['foe'];
            }else if (isset($_POST['sa7'])) {
                echo $newarr[6]['name'];
                echo $newarr[6]['degree'];
                echo $newarr[6]['foe'];
            }else if (isset($_POST['sa8'])) {
                echo $newarr[7]['name'];
                echo $newarr[7]['degree'];
                echo $newarr[7]['foe'];
            }else if (isset($_POST['sa9'])) {
                echo $newarr[8]['name'];
                echo $newarr[8]['degree'];
                echo $newarr[8]['foe'];
            }elseif (isset($_POST['sa10'])) {
                echo $newarr[9]['name'];
                echo $newarr[9]['degree'];
                echo $newarr[9]['foe'];
            }
            //session_destroy();
         ?>
    </body>
    </html>
0
Melvin Esguerra 9 Сен 2021 в 18:25

3 ответа

Лучший ответ

Привет, думаю, я понимаю, что вы пытаетесь сказать.

Просто оберните логику формы следующим образом:

<?php 

   if(isset($_POST['btn1'])) {
     //Code for form 1
   }

   if(isset($_POST['btn2'])) {
     //Code for form 2
   }
1
edluis97 9 Сен 2021 в 15:37

Вы пишете о необходимости доработки, я сообщил об этом. Ваш код очень популярен

0
UserOfStackOverFlow 9 Сен 2021 в 16:25

Извините за беспокойство, ребята, я нашел свою ошибку. это if else сеанса ['щелчок'], который запускает события, которые вызывают отправку указанной формы1. и спасибо за предложение сделать мои коды короче!

            <?php 
                include 'connection.php';
                session_start();
                $results_per_page = 10;
                if (!isset($_GET['page'])) {
                    $page = 1;
                }else{
                    $page = $_GET['page'];
                }
                $stmt1 = $conn->prepare("SELECT*FROM doctors"); 

                $stmt1->execute();

                $stmt2 = $conn->prepare("SELECT*FROM doctors ORDER BY doctor_name ASC");
                $stmt2->execute();

                while($row = $stmt2->fetch()) {
                    $result[] = [
                        'name' => $row['doctor_name'],
                        'degree' => $row['doctor_degree'],
                        'foe' => $row['doctor_foe'],
                    ];
                }
            ?>

            <!DOCTYPE html>
            <html>
            <head>
                <meta charset="utf-8">
                <title></title>
                <style type="text/css">
                    th, td{
                        width: 200px;
                        text-align: left;
                    }
                    div{
                        display: flex;
                    }
                </style>
            </head>
            <body>
                <div>
                    <form method="post">
                        <?php
                            $next = "<button class='btn-nav' name='plus'>Next</button>";
                            $prev = "<button class='btn-nav' name='min'>Prev</button>";

                            $next_un = "<button class='btn-nav' name='plus' disabled>Next</button>";
                            $prev_un = "<button class='btn-nav' name='min' disabled>Prev</button>";
                            $results = count($result);
                            $num_of_pages = ceil($results/$results_per_page);

                            $clicks = 0;
                            if (isset($_POST['plus'])) {
                                $_SESSION['clicks'] += 10 ;
                            }else if (isset($_POST['min'])){
                                $_SESSION['clicks'] -= 10;
                            }

                            if ($_SESSION['clicks']>0) {
                                echo $prev;
                            }else{
                                echo $prev_un;
                            }
                            if ($_SESSION['clicks']>=($results-10)) {
                                echo $next_un;
                            }else{
                                echo $next;
                            }

                            $newarr = array_slice($result, $_SESSION['clicks'], 10);
                        ?>
                    </form>
                </div>
                <div>
                    <table class="appt">
                        <tr>
                            <th>Doctor's Name</th>
                            <th>Educational Background</th>
                            <th>Field of Expertise</th>
                            <th>Set An Appointment</th>
                        </tr>
                        <?php 
                            foreach ($newarr as $row) {
                            echo "<tr><td>".$row['name']
                                ."</td><td>".$row['degree']
                                ."</td><td>".$row['foe']."</td></tr>";
                            }

                            echo $new_count = count($newarr);
                         ?>
                    </table>
                    <table class="appt">
                        <form method="post">
                            <tr><th>Set Appointment</th></tr>
                            <?php for ($i=1; $i < ($new_count+1); $i++): ?>
                                <?php echo '<tr><td><button id="submit" name="sa'.$i.'">Click me!</button></td></tr>'; ?>
                            <?php endfor ?>
                        </form>
                    </table>
                </div>
                <?php 
                    $sa1 = $_POST['sa1'] ?? '';
                    $sa2 = $_POST['sa2'] ?? '';
                    $sa3 = $_POST['sa3'] ?? '';
                    $sa4 = $_POST['sa4'] ?? '';
                    $sa5 = $_POST['sa5'] ?? '';
                    $sa6 = $_POST['sa6'] ?? '';
                    $sa7 = $_POST['sa7'] ?? '';
                    $sa8 = $_POST['sa8'] ?? '';
                    $sa9 = $_POST['sa9'] ?? '';
                    $sa10 = $_POST['sa10'] ?? '';


                    if (isset($_POST['sa1'])) {
                        echo $newarr[0]['name'];
                        echo $newarr[0]['degree'];
                        echo $newarr[0]['foe'];
                    }else if (isset($_POST['sa2'])) {
                        echo $newarr[1]['name'];
                        echo $newarr[1]['degree'];
                        echo $newarr[1]['foe'];
                    }else if (isset($_POST['sa3'])) {
                        echo $newarr[2]['name'];
                        echo $newarr[2]['degree'];
                        echo $newarr[2]['foe'];
                    }else if (isset($_POST['sa4'])) {
                        echo $newarr[3]['name'];
                        echo $newarr[3]['degree'];
                        echo $newarr[3]['foe'];
                    }else if (isset($_POST['sa5'])) {
                        echo $newarr[4]['name'];
                        echo $newarr[4]['degree'];
                        echo $newarr[4]['foe'];
                    }else if (isset($_POST['sa6'])) {
                        echo $newarr[5]['name'];
                        echo $newarr[5]['degree'];
                        echo $newarr[5]['foe'];
                    }else if (isset($_POST['sa7'])) {
                        echo $newarr[6]['name'];
                        echo $newarr[6]['degree'];
                        echo $newarr[6]['foe'];
                    }else if (isset($_POST['sa8'])) {
                        echo $newarr[7]['name'];
                        echo $newarr[7]['degree'];
                        echo $newarr[7]['foe'];
                    }else if (isset($_POST['sa9'])) {
                        echo $newarr[8]['name'];
                        echo $newarr[8]['degree'];
                        echo $newarr[8]['foe'];
                    }elseif (isset($_POST['sa10'])) {
                        echo $newarr[9]['name'];
                        echo $newarr[9]['degree'];
                        echo $newarr[9]['foe'];
                    }
                 ?>
            </body>
            </html>
0
Melvin Esguerra 9 Сен 2021 в 17:59