基于用户选择请求数据的php表单

时间:2014-04-20 16:28:37

标签: php forms

我已根据已收到的帮助对此进行了编辑。我似乎遇到了如何从数据库中调用信息的问题。我一直都会收到没有结果的错误。我想我很清楚可能是什么原因,但我不确定是否会纠正这个问题。

最初这个搜索功能设置不同。我有一个html页面,其中有一个下拉列表,每个选项都分配了一个值,即文本,即

<option Value="North East">North East</option>

但是因为我在数据库中有单独的表来控制下拉的总体,所以该值不再是文本而是数字。

关于如何打击这个的任何想法?

这是我从头到脚的目的。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <title>Results</title>
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
  <script type="text/javascript">
  $(document).ready(function()
  {
      $(".country").change(function()
      {
          var id=$(this).val();
          var dataString = 'id='+ id;

          $.ajax
          ({
              type: "POST",
              url: "ajax_city.php",
              data: dataString,
              cache: false,
              success: function(html)
              {
                  $(".city").html(html);
              } 
          });

      });
  });
  </script>
  <style>
  body {
      color: #FFFFFF;
  }
  #theclub{
      border:1px solid white;
      padding: 10px;
  }
  label
  {
      font-weight:bold;
      padding:10px;
  }
  </style>
  <link rel="stylesheet" type="text/css" href="Style.css">
  <script type='text/javascript' src='style2.js'></script>
 </head>
 <body>
  <form method="POST" enctype="multipart/form-data">
   <label>Country :</label> <select name="country" class="country">
   <option selected="selected">--Select Country--</option>
   <?php
   include('db.php');
   $sql=mysql_query("select id,data from data where weight='1'");
   while($row=mysql_fetch_array($sql))
   {
       $id=$row['id'];
       $data=$row['data'];
       echo '<option value="'.$id.'">'.$data.'</option>';
   } ?>
  </select>
  <label>City :</label> <select name="city" class="city">
   <option selected="selected">--Select City--</option>
  </select>
  <input type="submit" value="Search" name="submit">
 </form>
 <?php
 if(isset($_POST['submit']) && $_POST['submit'] != ""){ } //--here is the condition that is true if search button is pressed
 $selectedOption = $_POST["city"];

 $result = mysqli_query($con,
 sprintf("SELECT * FROM `SouthYorkshire` WHERE  `EstProv` = '%s'",
 preg_replace("/[^A-Za-z ]/", '', $selectedOption))); 

 echo "<div id=\"Results\">";

 while($row = mysqli_fetch_array($result))
 {
     echo "<div id=\"theclub\">";
     echo "<div class=\"ClubName\">";
     echo $row['EstName'];
     echo "</div><br>";
     echo "<div class=\"Location\">";
     echo $row['EstAddress2'];
     echo "</div>";
     echo "<br>";
     echo "<div id=\"website\"><a href=\"#\"><img src=\"photos/more-info.png\" width=\"75\" height=\"25\"/></a> <a href=\"" . $row['EstWebsite'] ."\" target=\"_blank\"><img src=\"photos/visit-website-button.png\" width=\"75\" height=\"25\" /></div></a></div>";
     echo "<br>";
 }
 echo "</div>";

 mysqli_close($con);
 ?>
 <br>
</body>

2 个答案:

答案 0 :(得分:0)

您的第二个代码应该包含在if条件中,以便在第一个代码清除之后运行它,如下所示:

但首先你必须在第一段代码中将名称属性添加到搜索按钮 -

<input type="submit" value="Search" name="submit">

现在你的第二个代码应该是 -

if($_POST['submit']) //--here is the condition that is true if search button is pressed
{
  $selectedOption = $_POST["city"];

$result = mysqli_query($con,
sprintf("SELECT * FROM `SouthYorkshire` WHERE  `EstProv` = '%s'",
    preg_replace("/[^A-Za-z ]/", '', $selectedOption))); 

echo "<div id=\"Results\">";

while($row = mysqli_fetch_array($result))
{
echo "<div id=\"theclub\">";
echo "<div class=\"ClubName\">";
echo $row['EstName'];
echo "</div><br>";
echo "<div class=\"Location\">";
echo $row['EstAddress2'];
echo "</div>";
echo "<br>";
echo "<div id=\"website\"><a href=\"#\"><img src=\"photos/more-info.png\" width=\"75\" height=\"25\"/></a> <a href=\"" . $row['EstWebsite'] ."\" target=\"_blank\"><img src=\"photos/visit-website-button.png\" width=\"75\" height=\"25\" /></div></a></div>";
echo "<br>";
}
echo "</div>";
mysqli_close($con);
?>
}

答案 1 :(得分:0)

我在黑暗中拍摄了一下,因为我不确定你的第一和第二代码是什么意思......

如果您只想运行一部分代码,则需要某种条件来检查是否运行它

如果您不希望代码的第二部分立即运行,您想要检查post变量city是否已设置且不为空,并且仅在值不为空时运行代码:

if(isset($_POST['city']) && $_POST['city'] != ""){
  // your code here
}