PHP级联下拉不起作用给出mysql_error

时间:2015-02-05 11:52:25

标签: javascript php jquery mysql ajax

我已经通过ajax构建了级联下拉,但它无法正常工作....

<tr>
<td width="31%" valign="top" bgcolor="#e2e2e2"><div align="right"><strong>Project</strong></div></td>
<td width="3%" valign="top" bgcolor="f2f2f2"><div align="center"><font color="#FF0000">*</font></div></td>
<td colspan="4" valign="top" bgcolor="f2f2f2">
<!-- Fetching project_name from db starts -->
<?php
                                                          $project_name=mysql_query("select main_unit_id,project_name from main_unit");
    ?>
<select name="project" id="project" tabindex="1">
 <option value=''>--Select Project --</option>
       <?php while($rows=mysql_fetch_array($project_name)) { ?>
 <option value="<?php echo $rows['main_unit_id']; ?>"><?php echo $rows["project_name"]; ?></option>
            <?php } ?>
     ?>
     </select>
       <!-- Fetching project_name from db ends -->
       <div></div>
              </td
                            </tr>
                            <tr>
       <td width="31%" valign="top" bgcolor="#e2e2e2"><div align="right"><strong>Project</strong></div></td>
       <td width="3%" valign="top" bgcolor="f2f2f2"><div align="center"><font color="#FF0000">*</font></div></td>
     <td colspan="4" valign="top" bgcolor="f2f2f2">
        <!-- Fetching activity from db starts -->
        <div id="activity"></div>
        <!-- Fetching activity from db ends -->
       </td
                            </tr>



 <script src="js/jquery-1.9.0.min.js"></script>
    <script language="javascript">
    $(document).ready(function(){
    $("select#project").change(function(){

  var main_unit_id =  $("select#project option:selected").attr('value');
  $("#activity").html( "" );
  if (main_unit_id.length > 0 ) {
    alert(main_unit_id);
   $.ajax({
      type: "POST",
      url: "fetch_activity.php",
      data: "main_unit_id="+main_unit_id,
      cache: false,
      beforeSend: function () {
        //$('#activity').html('<img src="img/loader.gif" alt="" width="24" height="24">');
      },
      success: function(html) {
        $("#activity").html(html);
      },
      error : function ($responseObj){
             alert("Something went wrong while processing your request.\n\nError => "
                 + $responseObj.responseText);
         }
    });
  }
});
});
</script>

和fetch_activity.php就是这个。

<?php
include "conn/conn.php";
$main_unit_id = trim(mysql_escape_string($_POST["main_unit_id"]));

$main_unit_id = mysql_real_escape_string($_POST['main_unit_id']);
$result = mysql_query("SELECT activity_id, activity_name FROM activities WHERE main_unit_id = '".$main_unit_id."'");

if($result === FALSE) {
    die(mysql_error()); // TODO: better error handling
}
?>
<select name="activity" id="activity">
  <option value="">-- Select Activity -- </option>
  <?php while($row = mysql_fetch_array($result)) { ?>
  <option value="<?php echo $row["activity_id"]; ?>"><?php echo $row["activity_name"]; ?></option>
  <?php } ?>
</select>
<?php
  }
?>

问题是在获取第一个下拉值后它没有显示下一个,我看到Chrome控制台也没有错误。

我在chrome developer工具中看到过,fetch_activity.php文件是请求中的接受数据,但没有返回任何响应。

0 个答案:

没有答案