我知道这个问题已经被问过很多次了,但是我已经尝试了好几个小时,却没有任何效果,我是php和ajax的新手,所以,我可能会遗漏一个我不知道的小东西,我想要在这里实现的目的是,我希望用户选择一个食物组,然后基于该组显示配料表。
我单独测试了process.php文件,它运行良好,还测试了脚本,发生了什么事,当我注释掉并键入alert(parent)时,从$ .ajax开头的行不起作用了,我实际上得到了选项的值,那么我在这里做错了什么?还是我缺少进口商品?PS我正在使用bootstrap 3.0进行设计
这是我的HTML代码
<!-- field food group--> <div class ="field form-group"> <label for="food-group"> Food Group * </label> <div class="input-group input-group-lg"> <select class="form-control" id="food-group-id" name="food-group" onchange="ajaxfunction(this.value)"> <?php // retreiving the recipe groups $RecipeGroup = new FoodGroup(); $data = $RecipeGroup->findAll(); foreach ($data->results() as $recipegroup) { // displaying the options echo '<option value="'.$recipegroup->food_group_id.'">'.$recipegroup->food_group_name.'</option>'; } ?> </select> </div> </div> <!-- field Ingredients--> <div class ="field form-group"> <label for="ingredients"> Ingredients * </label> <div class="input-group input-group-lg"> <select class="form-control" id="ingredients"> <?php // retreiving the recipe groups $ingredients = new Ingrident(); if(Input::exists()){ $data = $ingredients->findByGroup(Input::get('food-group')); }else $data = $ingredients->findByGroup(1); foreach ($data->results() as $ingredient) { // displaying the options echo '<option value="'.$ingredient->ingrident_id.'">'.$ingredient->ingrident_name.'</option>'; } ?> </select> <br> <br> <br> <span id="helpBlock" class="help-block center">Ingredient not listed ? <button class="btn btn-primary center" value="add-ing"> <span class="glyphicon glyphicon-plus"></span> Add New Ingredient </button> </span> </div> </div>
这是我的剧本
<script type="text/javascript"> function ajaxfunction(parent) { $.ajax({ url: 'process.php?food-group=' + parent; success: function(data) { $("#ingredients").html(data); } }); } </script>
这是我的process.php文件
<?php mysql_connect('localhost', 'root',''); mysql_select_db("online_recipes"); $result = mysql_query("SELECT * FROM `ingrident` WHERE `food_group_id_fk` = " . mysql_real_escape_string($_GET['food-group'])); while(($data = mysql_fetch_array($result)) !== false) echo '<option value="', $data['ingrident_id'],'">', $data['ingrident_name'],'</option>';
我的进口清单
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script> <script src="js/bootstrap.min.js"></script> <script src="js/docs.min.js"></script> <script src="js/ie10-viewport-bug-workaround.js"></script>
$.ajax({ url: 'process.php', type:'post', data: {parent: parent}, success: function(data) { $("#ingredients").html(data); } });
在您的process.php中
$parent = $_POST['parent']; echo($parent);