duankuangxie9070 2016-10-12 04:23
浏览 39

如何在单击搜索按钮并在同一页面上打印结果后保留搜索查询框和字母搜索

I have created my PHP page where I have searched query field and alphabetical search. After submitting query I am printing result on the same page. The query is working fine, but I want result to be displayed on the same place where I get result from alphabetical search. I.e. alphabetical search is being disappeared after the result comes from the search box. How can I retain both of them the in web page?

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="GET">
  <input type="text" name="query" />
  <input type="submit" value="Search" />
</form>
<?php
  session_start();  
  $con=mysqli_connect("localhost","root","","journalskart"); /*Database    Connection*/
      //include('inc/search.php');
      $errormsg= "No Record Found...!";
      //search box script start from here
      $alpha = $_GET['query'];
      $min_length = 3;
      if(strlen($alpha) >= $min_length){ // if query length is more or equal minimum length then

          $alpha = htmlspecialchars($alpha); 
          // changes characters used in html to their equivalents, for example: < to &gt;

          $alpha = mysqli_real_escape_string($con, $alpha);
          // makes sure nobody uses SQL injection
          $q1 = mysqli_query($con,"SELECT * FROM vendor1
          WHERE (title LIKE '%".$alpha."%') OR (publisher LIKE '%".$alpha."%')") or die(mysqli_error($con));    
          //$noofresult = mysqli_num_rows($q1);
         //echo "$noofresult Result Found<br/>";
         $num_rows=mysqli_num_rows($q1);

         if($num_rows > 0)
         { // if one or more rows are returned do following

              while($roww = mysqli_fetch_array($q1)){


               $_SESSION["Title"] = $roww['title']; 
               $_SESSION["Fullset"] = $roww["full_set"];
             $_SESSION["ISSN"] = $roww["issn"]; 
             $_SESSION["Publisher"] = $roww["publisher"]; 
             $_SESSION["Freq"] = $roww["freq"]; 
             $_SESSION["Volume"] = $roww["volume"]; 
             $_SESSION["Issue"] = $roww["issue"]; 
             $_SESSION["Year"] = $roww["year"]; 
                 $_SESSION["Price"] = $roww["price"]; 
             }
             }
             else{ // if there is no matching rows do following
              echo "No results";
            }   
            }//search box script end here
          else{ 




        $alpha="%";
        if (isset($_REQUEST['alpha'])) {
        $alpha = $_REQUEST['alpha']."%";
        }
        $q1 = mysqli_query($con, "select * from vendor1 where title like '$alpha%' " );

      ?>
<div >
  <form action="" method="post" >
    <table  >
      <tr>
        <td>
          <div  style="float:left;">
            <CENTER>
              <FONT COLOR=Green>Sort by Alphabet:</FONT>  
              <A HREF="<?php echo $_SERVER['PHP_SELF']; ?>" >All</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=A">A</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=B">B</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=C">C</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=D">D</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=E">E</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=F">F</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=G">G</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=H">H</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=I">I</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=J">J</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=K">K</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=L">L</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=M">M</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=N">N</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=O">O</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=P">P</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=Q">Q</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=R">R</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=S">S</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=T">T</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=U">U</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=V">V</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=W">W</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=X">X</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=Y">Y</A>
              <FONT COLOR=Green>|</FONT> <A HREF="?alpha=Z">Z</A>
            </CENTER>
          </div>
        </td>
      </tr>
      <tr>
        <th>Title</th>
        <th>Full Set</th>
        <th>ISSN</th>
        <th>Publisher</th>
        <th>Freq</th>
        <th>Volume</th>
        <th>Issue</th>
        <th>Year</th>
        <th>Price</th>
      </tr>
      <tbody>
        <?php
          $i=1;
          $num_rows=mysqli_num_rows($q1);

          if($num_rows==0)
          {
          ?>
        <div align="center">
          <div class="alert alert-danger alert-dismissable">
            <?php echo $errormsg; ?>
          </div>
        </div>
        <?php
          }
          else
          {
          while($roww=mysqli_fetch_array($q1))
          {   
          $_SESSION["Title"] = $roww['title']; 
                   $_SESSION["Fullset"] = $roww["full_set"];
                 $_SESSION["ISSN"] = $roww["issn"]; 
                 $_SESSION["Publisher"] = $roww["publisher"]; 
                 $_SESSION["Freq"] = $roww["freq"]; 
                 $_SESSION["Volume"] = $roww["volume"]; 
                 $_SESSION["Issue"] = $roww["issue"]; 
                 $_SESSION["Year"] = $roww["year"]; 
                     $_SESSION["Price"] = $roww["price"];

            ?>
        <tr>
          <td>
            <div style="height:100px;float:left;">
              <input name="" type="checkbox" value="">
            </div>
            <div >
              <a href="#">  <b><?php echo $_SESSION["Title"]; ?></b></a>
          <td><?php echo $_SESSION["Fullset"]; ?></td>
          <td><?php echo $_SESSION["ISSN"]; ?></td>
          <td><?php echo $_SESSION["Publisher"]; ?></td>
          <td><?php echo $_SESSION["Freq"]; ?></td>
          <td><?php echo $_SESSION["Volume"]; ?></td>
          <td><?php echo $_SESSION["Issue"]; ?></td>
          <td><?php echo $_SESSION["Year"]; ?></td>
          <td><?php echo $_SESSION["Price"]; ?></td>
          </div></td>
        </tr>
        <?php }
          $i++;
          }
          }
          echo "$num_rows Results Found";
          ?>
      </tbody>
    </table>
  </form>
</div>
</body></html>
<SCRIPT LANGUAGE=JavaScript>
  <!--
    document.write(ALPHABET())
    //-->
</SCRIPT>
  • 写回答

1条回答 默认 最新

  • double2022 2016-10-12 08:53
    关注

    Try using a condition based check to see whether a search has been made

    <?php  
    //general declarations
    
    if ($_SERVER["REQUEST_METHOD"] == "GET") {  
        //body of your php code execution  
    }  
    ?>php
    
    <html>  
    //your html body  
    </html>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法