动态地改变php我的sql中的下拉

时间:2017-01-11 08:25:59

标签: php ajax

我有两个下拉列表。有一个静态值,第二个从db获取值。我希望如果从第1个下拉列表中选择值,则在第2个下拉列表中加载相关值。我试过了。但是它根据用户加载来自数据库的所有数据。例如,当用户从具有值查询的请求类型下拉列表中选择时,然后第二次下拉加载仅具有catType Inquiry的值。如果他选择了投诉,则必须显示投诉数据我已经尝试了但是所有的数据都被加载了,或者只有一个数据正在加载。在这方面,我的身体对我有所帮助。谢谢你。这是我的代码

   <div class="col-md-4">
<div class="form-group">
<label for="requesttype"><?php echo $requestField; ?></label>
<select class="form-control" required="" id="requesttype" name="requesttype" onchange="fcrActionChange(this);">                      
<option value="">Select Request Type</option>
<option value="Inquiry">Inquiry</option>
<option value="Complaint">Complaint</option>
<option value="Service Request/FCR">Service Request/FCR</option>
<option value="Verification Call">Verification Call</option>
</select>
<span class="help-block"><?php echo $requestHelp; ?></span>
</div>
 </div>

$("#requesttype").change(function() {
$("#catId).load("navigation.php?requesttype=" + $("#requesttype").val());
  });
   </script>

 <div class="col-md-4">
<div class="form-group">
<label for="catId"><?php echo $categoryField; ?></label>
 <select class="form-control" name="catId" id="catId">
 $tcat = "SELECT catId, catName FROM categories WHERE userId = ".$userId." AND isActive = 1 AND catType = ".$_GET['requesttype'];
 $rest = mysqli_query($mysqli, $tcat) or die('-2'.mysqli_error());
 while ($tcatrow = mysqli_fetch_assoc($rest)) { 
  echo "<option value="$tcatrow['catId'] >";
  echo     clean($tcatrow['catName'])."</option>";
 }
 </select>
<span class="help-block"><?php echo $categoryHelp; ?></span>
</div>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

您的代码非常令人困惑,但无论如何,重要的部分是在您的ajax请求和您的PHP文件中

实施例。您可以根据secondOpt的查询填写div requesttype

**注意我添加了userId输入字段,以便在navigation.php

中传递SQL查询的值
 <select class="form-control" required="" id="requesttype" name="requesttype" onchange="fcrActionChange(this);">                      
 <option value="">Select Request Type</option>
 <option value="Inquiry">Inquiry</option>
 <option value="Complaint">Complaint</option>
 <option value="Service Request/FCR">Service Request/FCR</option>
 <option value="Verification Call">Verification Call</option>
 </select>
 <input type="hidden" id="userId" value="<?php echo $userId;?>">
 <div id="secondOpt"></div>

在此之后,您将在下面有一个ajax请求,您可以使用jQuery中的$.post并在secondOpt元素上呈现返回的数据

 $('#requesttype').change(function(){
    $.post("navigation.php",{requesttype: $(this).val(),userId: $('#userId').val()},function(options)
    {
       $('#secondOpt').html(options);
    });
 });

适用于navigation.php 更新

 //don't forget your config file here to connect with the database

 $userId = mysql_real_escape_string($_POST['userId']);
 $requesttype = mysql_real_escape_string($_POST['requesttype']); 

 $output = "<select id='catId'>";

 $tcat = "SELECT catId, catName FROM categories WHERE userId = ".$userId."  AND isActive = 1 AND catType = ".$requesttype;
 $rest = mysqli_query($mysqli, $tcat) or die('-2'.mysqli_error());
 while ($tcatrow = mysqli_fetch_assoc($rest)) { 
   $output.="<option value=".$tcatrow['catId']." >";
   $output.=clean($tcatrow['catName'])."</option>";
  }
 $output.="</select>";


 echo $output;