SWITCH / CASE Multiple Submit Buttons with PHP using POST method

时间:2022-04-17 07:57:03

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

首先,我是PHP的菜鸟和使用Forms,但我正在学习,因为我去!其次,我知道这已被多次覆盖,但我无法让它对我有用!这可能只是我忽略的东西......

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

我有一个包含1个搜索字段和3个提交按钮的表单。根据所选的按钮,查询将在数据库中查找3个不同搜索之一。这很简单......

http://1karaokedj.com

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()"

这总是不返回找到的记录。如果我添加一个默认情况,那么它的工作原理是print_r($ _ POST);返回“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>

1 个解决方案

#1


1  

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.

这是因为下面的行在提交表单时重定向。重定向时不保留POST值。因此,$ _POST ['searchbtn']为空,在switch语句中它将转为默认情况。

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

#1


1  

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.

这是因为下面的行在提交表单时重定向。重定向时不保留POST值。因此,$ _POST ['searchbtn']为空,在switch语句中它将转为默认情况。

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