微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 动态选择选项php和mysql

我知道这个问题已被多次询问,但我已经尝试了几个小时但没有任何效果,我是一个PHP和ajax的菜鸟,所以,我可能会错过一个我不知道的小事,我是什么的试图在这里实现的是,我希望用户选择一个食物组,并根据该组成一系列成分.

我单独测试了我的process.PHP文件,它运行良好,我还测试了脚本会发生什么,当我将它注释掉并键入alert(parent)时,从$.ajax开始的行不起作用我实际上得到了选择的选项的价值,所以我在这里做错了什么?或者我错过了一个导入?
P.S我正在使用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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐