Thamus
Thamus

Reputation: 108

2 submit buttons on html form

I have 2 submit buttons on 1 form

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

now my problem is when i submit the form using the 2nd submit button my php code displays a blank data.

here's my php code

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


    }

}

}

?> 

My Objective : I want the user to enter a search string, and if the user cannot find what he/she wants on the results they can filter the data retrieved furthermore.

Problem : when the user submits the form using the first submit button the results shows up after that when they submit the form using the 2nd submit button the results are gone.

Upvotes: 0

Views: 656

Answers (4)

Sougata Bose
Sougata Bose

Reputation: 31749

Add name to second submit button.

<input name="buttonname">

And check on main.php -

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

Upvotes: 1

Banshi Lal Dangi
Banshi Lal Dangi

Reputation: 675

There is no any name associated with #2 SUBMIT button, you must have to give a name to it. Example- add name="filter_btn" to second submit button, after adding the name the submit button will look like this.

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

And then modify the code into the PHP in following manner.

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

Upvotes: 2

Iswanto San
Iswanto San

Reputation: 18569

Your PHP code request a $_POST['search'] value.

But in your second search you give the textbox name as filter

Try to change your PHP code like this:

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

Or

set your second textbox name as search

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

Upvotes: 0

AlhasanIQ
AlhasanIQ

Reputation: 183

Before the second submit button . The input tag is not closed

Close it then tell me if it is the same

Upvotes: 0

Related Questions