使用 php/javascript 或 jquery 提交 form2 时阻止其他 form1 提交

Posted

技术标签:

【中文标题】使用 php/javascript 或 jquery 提交 form2 时阻止其他 form1 提交【英文标题】:stop other form1 from submitting when submitting form2 using php/javascript or jquery 【发布时间】:2021-11-06 06:44:04 【问题描述】:

这是我正在做的真正代码 第一篇文章包含同样的问题,

如您所见,表单 1 按钮用于控制 $_SESSION['clicks'] 值的迭代,我在此表单中使用会话,

表单 2 按钮用于回显从另一个数组中检索到的对应名称的数组。

问题是每次在 php 中单击或调用 form2 中的按钮时,其他表单或 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>

【问题讨论】:

你所说的听起来不正确。您可以在页面中拥有任意数量的表单,但用户一次只能提交一个。提交一份表格不会导致提交另一份表格。唯一的例外是,如果您专门编写了一些导致这种情况发生的 javascript 代码……您这样做了吗? 对不起,我更新了我的帖子,你能看一下吗? 【参考方案1】:

嗨,我想我明白你想说什么。

只需使用以下内容包装表单中的逻辑:

<?php 

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

   if(isset($_POST['btn2'])) 
     //Code for form 2
   

【讨论】:

虽然你最终可能是对的,但这有点投机。我们不确定 PHP 如何处理表单,以及这是否导致了 OP 所看到的任何内容(不清楚,因为它们的描述似乎在谈论一些不可能的事情)。最好提供依赖于问题信息而不是猜测的答案。获得 50 名代表,发表评论要求澄清,关注问题,然后在确定具体问题后回答。 是的,我正在使用会话,这会影响其他表单的性能吗? @MelvinEsguerra 之前没有提到会话......而且我的 sn-p 与会话无关......顺便说一下,你熟悉循环(for、foreach、while)不需要所有这些回声,您只需关闭 php 标签写入 html 并重新打开 php 标签。输入名称也可以是数组,所以你不会有 sa1 ... sa10... @edluis97 抱歉,如果您认为我在“展示我的代表”,但实际上不是,我只是想在网站上保持高质量的答案。与个人无关 :-)。我没有说你不能帮忙。但是......过于依赖猜测/假设的答案通常最终会成为无益的分心。在***,我们更愿意尽可能根据可用的事实来回答。如果事实不可用,请要求澄清。 50 rep 是 5 upvotes,所以这不是一个漫长的旅程,即使你不能给太多时间。我不会在这里花太多时间,我只是在这里待了一段时间!【参考方案2】:

很抱歉打扰你们,我发现我的错误。是 session['click'] 的 if else 触发了导致上述 form1 被提交的事件。并感谢您让我的代码更短的建议!

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

【讨论】:

不客气,@Melvin。很抱歉,我无法提供更多帮助。祝你在这条漫长(真的很长)的道路上好运。继续练习,尽量不要太不知所措。一步一步走很长的路。 非常感谢!我会记住这一点

以上是关于使用 php/javascript 或 jquery 提交 form2 时阻止其他 form1 提交的主要内容,如果未能解决你的问题,请参考以下文章

使用 php/javascript 或 jquery 提交 form2 时阻止其他 form1 提交

JQuer.HoverDir的基本使用方法

50k点的图形库? PHP 或 Javascript

jquer和封装的运动函数对比

Jquer好友DIV改变颜色

jquer 选择器