2016-05-12 122 views
0

道歉,如果这是一个业余爱好者的问题,我的PHP技能仍在开发中。我目前使用当前代码创建了一个搜索框。PHP通过mysql查询结果显示在另一页上

<?php  
     // Tyre search setup 
     $profileSql = "SELECT DISTINCT `profile` FROM `tyres` WHERE `profile` > 0 ORDER BY `profile`"; 
     $profileResult = $db->query($profileSql); 

     $widthSql = "SELECT DISTINCT `width` FROM `tyres` WHERE `width` > 0 ORDER BY `width`"; 
     $widthResult = $db->query($widthSql); 

     $diamSql = "SELECT DISTINCT `diam` FROM `tyres` WHERE `diam` > 0 ORDER BY `diam`"; 
     $diamResult = $db->query($diamSql); 

     if(isset($_GET['profile']) && isset($_GET['width']) && isset($_GET['diam'])) { 
      $profile = $_GET['profile']; 
      $width = $_GET['width']; 
      $diam = $_GET['diam']; 

      $tyreSql = "SELECT * FROM `tyres` WHERE `profile` = " . $profile . " AND `width` = " . $width . " AND `diam` = " . $diam . " AND rrp > 0 ORDER BY `profile`"; 
      $tyreResult = $db->query($tyreSql); 
     } 
?> 

下面是这种逻辑所绑定的形式。

<form id="formTyreSearch" name="tyreSearch" method="GET"> 
    <h2>Tyre Search</h2> 
    <p>Use our search below to find the tyre for your car.</p> 
     <div class="form-group">Tyre Profile: 
      <select name = "diamSearch"> 
       <?php while($row=mysqli_fetch_assoc($profileResult)) : ?> 
        <option value="<?php echo $row['profile']; ?>"><?php echo $row['profile']; ?></option> 
       <?php endwhile; ?> 
      </select> 
      </div> 

     <div class="form-group">Tyre Width: 
      <select name = "pcdSearch"> 
       <?php while($row=mysqli_fetch_assoc($widthResult)) : ?> 
        <option value="<?php echo $row['width']; ?>"><?php echo $row['width']; ?></option> 
       <?php endwhile; ?> 
      </select> 
     </div> 

     <div class="form-group">Tyre Diam: 
      <select name = "pcdSearch"> 
       <?php while($row=mysqli_fetch_assoc($diamResult)) : ?> 
        <option value="<?php echo $row['diam']; ?>"><?php echo $row['diam']; ?></option> 
       <?php endwhile; ?> 
      </select> 
     </div> 

     <button type="submit" name="filterOptions" value="displayManu" class="btn btn-default btn-sm btn-primary"><i class="fa fa-pencil"></i> Search Now</button> 
</form> 

现在,从这里我假设的形式选择的值在$ tyreResult变量设置。

我想将此结果传递给tyres.php页面并在那里显示搜索结果,那么执行此操作的最佳做​​法是什么?以及如果用户没有选择所有三个值,我应该如何处理,因为网站上会有其他内容,我不想重新加载页面并在顶部显示错误...

+0

我建议你的结果转换成JSON并使用POST将数据发送到其他页面。你不需要一个表格。只需获取JSON并在结果对象中再次进行转换即可。查看http://php.net/manual/en/book.json.php以了解如何管理PHP中的JSON数据。然后检查这个http://php.net/manual/en/book.curl.php curl将JSON数据发布到另一个PHP页面。 – mhyst

回答

1

只需在表单中添加action属性并将结果逻辑移动到tyres.php即可。如果他们没有选择所有值,则将其重定向回到上一页。但是,除非用户恶作剧地改变你的HTML,否则不应该发生。默认情况下,您的<select>标签的第一个选项将被选中。

的search.php

<?php  
// Tyre search setup 
$profileSql = "SELECT DISTINCT `profile` FROM `tyres` WHERE `profile` > 0 ORDER BY `profile`"; 
$profileResult = $db->query($profileSql); 

$widthSql = "SELECT DISTINCT `width` FROM `tyres` WHERE `width` > 0 ORDER BY `width`"; 
$widthResult = $db->query($widthSql); 

$diamSql = "SELECT DISTINCT `diam` FROM `tyres` WHERE `diam` > 0 ORDER BY `diam`"; 
$diamResult = $db->query($diamSql); 
?> 

<form id="formTyreSearch" method="get" action="tyres.php"> 
    <h2>Tyre Search</h2> 
    <p>Use our search below to find the tyre for your car.</p> 
    <div class="form-group"> 
     Tyre Profile: 
     <select name="profile"> 
      <?php while ($row = mysqli_fetch_assoc($profileResult)) : ?> 
       <option value="<?php echo $row['profile'] ?>"><?php echo $row['profile'] ?></option> 
      <?php endwhile ?> 
     </select> 
    </div> 
    <div class="form-group"> 
     Tyre Width: 
     <select name="width"> 
     <?php while ($row = mysqli_fetch_assoc($widthResult)) : ?> 
      <option value="<?php echo $row['width'] ?>"><?php echo $row['width'] ?></option> 
     <?php endwhile ?> 
     </select> 
    </div> 
    <div class="form-group"> 
     Tyre Diam: 
     <select name="diam"> 
     <?php while ($row = mysqli_fetch_assoc($diamResult)) : ?> 
      <option value="<?php echo $row['diam'] ?>"><?php echo $row['diam'] ?></option> 
     <?php endwhile ?> 
     </select> 
    </div> 
    <button type="submit" name="filterOptions" value="displayManu" class="btn btn-default btn-sm btn-primary"> 
     <i class="fa fa-pencil"></i> Search Now 
    </button> 
</form> 

tyres.php

<?php 
if(isset($_GET['profile'], $_GET['width'], $_GET['diam'])) { 
    $profile = $_GET['profile']; 
    $width = $_GET['width']; 
    $diam = $_GET['diam']; 

    $tyreSql = "SELECT * FROM `tyres` WHERE `profile` = " . $profile . " AND `width` = " . $width . " AND `diam` = " . $diam . " AND rrp > 0 ORDER BY `profile`"; 
    $tyreResult = $db->query($tyreSql); 
} else { 
    header('Location: search.php'); 
    exit; 
} 
?> 

<!-- show the results --> 
+0

Hey Mikey,似乎没有通过值,只有tyres.php?tyreSearch =在标题中。也无法使用标题('Location:'),因为我得到这个错误警告:无法修改标题信息 - 已在第55行的\ tyres.php中发送的标题(输出开始于includes \ quickModalTyres.inc.php:127) 55是头文件,第127行是<?php echo ob_get_clean(); ?> – Jcode

+0

这很奇怪。我期望'tyres.php?pcdSearch ='基于你的原始代码。您需要为您的'