Search code examples
phpformssubmit-button

SWITCH / CASE Multiple Submit Buttons with PHP using POST method


First, I'm a noob in PHP and using Forms, but I'm learning as I go! Second, I know this has been covered many times, but I just can't make it work for me! It's probably just something I'm overlooking...

I have a form with 1 search field and 3 submit buttons. Depending on the button selected, the query will look in the database for one of 3 different searches. It's fairly simple...

http://1karaokedj.com

<input name="searchterm" type="search" placeholder="Enter Search Terms Here"  class="search" />
<input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Artist Search"  />
<input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Title Search"  />
<input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Disc ID Search"  />
<?php if(isset($_SESSION['searchterm'])) {
          if(($_SESSION['searchterm']!="")) {
            switch ($_POST['searchbtn']) {
              case 'Artist Search':
                $searchresults=$db->query("select * from 1KaraokeDJ where Artist like '%$searchterm%' limit 100");
                break;
              case 'Title Search':
                $searchresults=$db->query("select * from 1KaraokeDJ where Title like '%$searchterm%' limit 100");
                break;
              case 'Disc ID Search':
                $searchresults=$db->query("select * from 1KaraokeDJ where Disc like '%$searchterm%' limit 100");
                break;
            }

            if(mysqli_num_rows($searchresults) > 0) {
...
}}}?>

this always returns no records found. if I add a default case then it works print_r($_POST); returns "Array()"

here is the full code in case the problem is elsewhere:

<?php
  include("connect.php");
  require_once 'Mobile_Detect.php';
  $detect = new Mobile_Detect;
  ini_set('session.cookie_lifetime',900);
  ini_set('session.gc_maxlifetime',900);
  session_start();
  if(isset($_POST['searchbtn'])) {
    $_SESSION['searchterm']=$_POST['searchterm'];
    if(($_SESSION['searchterm'])!="") { header("location:index.php"); }
    else { echo "<script> alert('Please enter something to search for') </script>"; }
  }
?>
<html>
  <head>
    <title>1KaraokeDJ.com Search</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <form method="post">
        <p><code>
          <img src="top1.jpg" /><br>
          <?php if ($detect->isMobile()) { echo("Mobile Device Detected"); } else { echo("Desktop Browser Detected"); } ?>
        </code></p>
        <p>
          <?php if(isset($_SESSION['searchterm'])) { ?>
            <input name="searchterm" type="search" value="<?php echo $_SESSION['searchterm'];?>" class="search" />
          <?php } else { ?>
            <input name="searchterm" type="search" placeholder="Enter Search Terms Here"  class="search" />
          <?php } ?>
        </p>
        <p>
          <input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Artist Search"  />
          <input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Title Search"  />
          <input name="searchbtn" type="submit" id="searchbtn" class="searchbutton" value="Disc ID Search"  />
        </p>
        <hr style="width:100%">
        <?php if(isset($_SESSION['searchterm'])) {
          if(($_SESSION['searchterm']!="")) {
            $searchterm=strtoupper($_SESSION['searchterm']);

            print_r($_POST);

            switch ($_POST['searchbtn']) {
              case 'Artist Search':
                $searchresults=$db->query("select * from 1KaraokeDJ where Artist like '%$searchterm%' limit 100");
                break;
              case 'Title Search':
                $searchresults=$db->query("select * from 1KaraokeDJ where Title like '%$searchterm%' limit 100");
                break;
              case 'Disc ID Search':
                $searchresults=$db->query("select * from 1KaraokeDJ where Disc like '%$searchterm%' limit 100");
                break;
            }

            if(mysqli_num_rows($searchresults) > 0) {
              while($descri=mysqli_fetch_object($searchresults)) { ?>
        <div class="reslt">
          <h3 id="results">
            <?php
              echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Artist);
              echo " - ";
              echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Title);
            ?>
          </h3>
          <p class="Description">
            <?php
              echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Brand);
              echo " - ";
              echo str_ireplace($searchterm, '<span class="highlight">'.$searchterm."</span>", $descri->Disc);
              echo " - ";
              echo $descri->Track;
            ?>
          <p>
          <hr>
        </div>
        <?php } ?>
        <div class="reslt">
          <h3 id="results"><?php echo mysqli_num_rows($searchresults) ?> Results</h3>
          <?php if(mysqli_num_rows($searchresults) >= 100) { ?>
            <p class="Description highlight">Showing Up To 100 Results<br>Try Refining Your Search</p><hr> 
          <?php } ?>
        </div>
        <?php   } else { ?>
        <div class="reslt">
          <h3 id="results">Nothing Found!</h3>
          <p class="Description highlight">Try Changing Your Search Terms<p><hr>
        </div>
        <?php } } } ?>
      </form>
      <code>This site is in testing... Things may change anytime<br>and many things may not work for a while!</code>
    </div>
  </body>
</html>

Solution

  • This is because of below line that redirects when the form is submitted. POST values are not retained when redirected. Hence, $_POST['searchbtn'] is empty and in switch statement it goes to default case.

    header("location:index.php");
    

    Comment header redirection and it should work.

    Updated code:

    if(isset($_POST['searchbtn'])) {
        $_SESSION['searchterm']=$_POST['searchterm'];
        if(($_SESSION['searchterm'])!="") { /*header("location:index.php");*/ }
        else { echo "<script> alert('Please enter something to search for') </script>";}
    }