2在html表单上提交按钮

时间:2015-04-17 05:01:06

标签: php html forms

我在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>";


    }

}

}

?> 

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

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

4 个答案:

答案 0 :(得分:2)

没有与#2 SUBMIT按钮关联的任何名称,您必须为其命名。 示例 - 将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
}

答案 1 :(得分:1)

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

<input name="buttonname">

并查看main.php -

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

您的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" ...