Skip to content
Advertisement

stop other form1 from submitting when submitting form2 using php/javascript or jquery

this is the real code I am doing the first post contains the same problem,

as you can see the form 1 buttons is to control the iteration of the $_SESSION[‘clicks’] value i am using sessions in this form,

the form 2 buttons are to echo out the array for corresponding names that are retrieved from another array.

and the problem is every time the buttons in form2 is clicked or called out in php the other form or form1 gets submitted too

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

Advertisement

Answer

Hi think I understand what you’re trying to say.

just wrap the logic from your form with the following:

<?php 

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

   if(isset($_POST['btn2'])) {
     //Code for form 2
   }
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement