【问题标题】:2 submit buttons on html form2 html表单上的提交按钮
【发布时间】:2022-01-24 12:16:51
【问题描述】:

我在 1 个表单上有 2 个提交按钮

<form action="main.php" method="POST" id="form" name="form">
    <div id="box" name="box" style="-webkit-transition:1s;overflow:hidden;">
        <label class="boxes"><input type="checkbox" id="All" name="All" onclick="checkAll()" /><span>All</span></label>
        <label class="boxes"><input type="checkbox" id="books" name="books" onclick="rmOthers(1)"/><span>Books</span></label>
        <label class="boxes"><input type="checkbox" id="journals" name="journals" onclick="rmOthers(2)"/><span>Journals</span></label>
        <label class="boxes"><input type="checkbox" id="guidelines" name="guidelines" onclick="rmOthers(3)"/><span>Guidelines</span></label>
        <label class="boxes"><input type="checkbox" id="pe" name="pe" onclick="rmOthers(4)"/><span>Patient Education</span></label>
        </div>
        <button type="button" class="submit" id="hide" style="padding:15px 20px;"  onclick="hideBoxes()">+</button><input type="search" name="search" class="inputs" placeholder="Search Articles" value="<?php echo isset($_POST['search']) ? $_POST['search'] : ""; ?>" /><button type="submit" name="submit" class="submit" >Search</button><br />
        <div id="sq">
            <select id="sqType" style="width:5%;">
            <option class="ar">And</option>
            <option class="ar">Or</option>
            </select>
            <input type="text" class="inputs" placeholder="Filter Results" name="filter" style="width:70%;" value='<?php echo (isset($_POST['filter']) ? $_POST['filter'] : ''); ?>'/>
            <button type='submit' id="fil" class="submit" style="-webkit-transform:translateX(-5px) translateY(-3px); width:7%;" >Filter</button>
        </div>
    </form>

现在我的问题是当我使用第二个提交按钮提交表单时,我的 php 代码显示空白数据。

这是我的 php 代码

if(isset($_POST['submit']))
{
$search = $_POST['search'];

$sql = (strcmp($search, "") == true ? "SELECT * FROM `tbl_article_abstract` WHERE `title` LIKE '%$search%'" : "SELECT * FROM `tbl_article_abstract` WHERE `title` LIKE '%$search%' LIMIT 30");

$query = mysqli_query($con, $sql);
if(mysqli_num_rows($query) != 0)
{

    while($run = mysqli_fetch_assoc($query))
    {   

        $title = (strlen($run['title']) > 121) ? substr($run['title'], 0, strpos(wordwrap($run['title'], 121), "\n")) . '...' : $run['title'];

echo '<div class="SearchResults">';
echo "      <span class='top'>";
echo "          <a>";
echo "              <h3>". strtoupper($title) ."</h3>";
echo "          </a>";
echo "          <br />";
echo "          <h5 class='sub'>";
echo "          Authors :<a class='authors'>Dr.Michael Ramsay</a><a class='authors'>Dr.Lars Benitez</a><a class='authors'>Dr.Kevin John Pascual</a><br><br>";
echo "          </h5>";
echo "      </span>";
echo "      <span class='bottom'>";
echo "          <span class='bottomLeft'>";
echo                    ($run['abstract'] != "" ? "             <a class='options' onclick='showOverlay(". $run['id'] .")'>Abstract</a><span style='margin:0px 5px;'>|</span>" : "" );
echo "              <a target='_blank' href='view.php?filename=NKTI  Proceedings  vol. 1 no. 1 Feb.  1996' class='options'>";
echo "                  Full Article";
echo "              </a>";  
echo "          </span>";
echo "          <div class='overlay' id='". $run['id'] ."' onclick='hideOverlay(this, event)'> ";
echo "              <iframe class='abstract' src='abstract.php?id=".$run['id']."' style='padding:0px;' scrolling='no'>";
echo "                      <button class='closeButton' onclick='hideOverlay(". $run['id'] .")'>x</button>";
echo "                      <div class='wrapper'><h3>".$run['title']."</h3></div><br />";
echo                        $run['abstract'];
echo "              </iframe>";
echo "          </div>";
echo "          <span class='bottomRight'>";
echo "              <p class='label'>NKTI Proceedings volume ".$run['volume'].", January - April 2015 @ Pg. 1-15</p>";
echo "          </span>";
echo "      </span>";
echo "      <br style='clear:both;'/>";
echo "</div>";


    }

}

}

?> 

我的目标:我希望用户输入一个搜索字符串,如果用户在结果中找不到他/她想要的内容,他们可以进一步过滤检索到的数据。

问题:当用户使用第一个提交按钮提交表单时,结果显示,之后当他们使用第二个提交按钮提交表单时,结果消失了。

【问题讨论】:

    标签: php html forms


    【解决方案1】:

    #2 提交按钮没有任何名称,您必须为其命名。 示例 - 将 name="filter_btn" 添加到第二个提交按钮,添加名称后提交按钮将如下所示。

    <button type='submit' name='filter_btn' id="fil" class="submit" style="-webkit-transform:translateX(-5px) translateY(-3px); width:7%;" >Filter</button>
    

    然后按照以下方式将代码修改成PHP。

    if((isset($_POST['submit'])) || (isset($_POST['filter_btn'])))
    {
     //your code goes here
    }
    

    【讨论】:

      【解决方案2】:

      为第二个提交按钮添加名称。

      <input name="buttonname">
      

      并检查 main.php -

      if (isset($_POST['buttonname'])) {
          echo "Say Hello";
      }
      

      【讨论】:

        【解决方案3】:

        在第二个提交按钮之前。输入标签未关闭

        关闭它然后告诉我是否相同

        【讨论】:

          【解决方案4】:

          您的 PHP 代码请求 $_POST['search'] 值。

          但在您的第二次搜索中,您将文本框名称指定为 filter

          尝试像这样更改您的 PHP 代码:

          $search = $_POST['search'];
          if(!isset($search) || $empty($search))
          {
              $search = $_POST['filter'];
          }
          

          或者

          将您的第二个文本框名称设置为搜索

           <input type="text" class="inputs" placeholder="Filter Results" name="search" ...
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-02-27
            • 1970-01-01
            • 2010-10-11
            • 2011-02-19
            相关资源
            最近更新 更多