动态选择PHP和MySQL选项 [英] Dynamic select options php and mysql

查看:148
本文介绍了动态选择PHP和MySQL选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经被问了很多次,但我已经尝试过了几个小时并没有什么工作,我与PHP和AJAX这么一个小白,我可能会丢失一个小的事情,我不知道,是什么我想在这里实现的是,我希望用户选择一个食品集团,并基于该成分的列表会显示出来。

我单独测试我的process.php文件,它运作良好,我也测试了发生的事情是,从$就开始行,当我把它注释掉和类型的警报(父),我不工作的脚本实际上得到所选择的选项的值,所以我究竟做错了什么?还是我失去了一个导入? P.S我使用的引导3.0的设计

下面是我的html code

 <  - !外地食品集团 - >
          < D​​IV CLASS =字段的形式组>
            <标签=食品组>食品集团*< /标签>
            < D​​IV CLASS =输入组输入组LG>

            <选择类=表单控制ID =食品组IDNAME =食团的onchange =ajaxfunction(THIS.VALUE)>
              < PHP
              在检索//配方组
                  $ RecipeGroup =新FoodGroup();
                  $数据= $ RecipeGroup->的findAll();

                  的foreach($数据 - >结果()为$ recipegroup){
                    //显示的选项
                    回声'<期权价值='$ recipegroup-> food_group_id。'>'。$ recipegroup-> food_group_name。'< /选项>';
                  }
               ?>

            < /选择>

            < / DIV>
          < / DIV>
          <  - !外地成分 - >
          < D​​IV CLASS =字段的形式组>
            <标签=成分>配料*< /标签>
            < D​​IV CLASS =输入组输入组LG>

            <选择类=表单控制ID =成分>

               < PHP
              在检索//配方组
                  $成分=新Ingrident();
                  如果(输入::存在()){
                    $数据= $ ingredients-> findByGroup(输入::得到('食品集团'));
                  }其他
                  $数据= $ ingredients-> findByGroup(1);

                  的foreach($数据 - >结果()为$成分){
                    //显示的选项
                    回声'<期权价值='$ ingredient-> ingrident_id。'>'。$ ingredient-> ingrident_name。'< /选项>';
                  }
               ?>

            < /选择>
            < BR> < BR> < BR>
            <跨度ID =helpBlock级=帮助块中心>成份不上市?
              <按钮类=BTN BTN-主中心的价值=添加 - 荷兰国际集团> <跨度类=glyphicon glyphicon加>< / SPAN>添加新的成分和LT; /按钮>

            < / SPAN>

            < / DIV>
          < / DIV>
 

下面是我的脚本

 <脚本类型=文/ JavaScript的>
  功能ajaxfunction(母公司)
  {


    $阿贾克斯({
          网址:process.php食品组=?'+父母;
          成功:功能(数据){
              $(#成分)的HTML(数据)。
          }
      });
  }
< / SCRIPT>
 

下面是我的process.php文件

 < PHP
    的mysql_connect('本地主机,根,'');
    mysql_select_db(online_recipes);

    $结果= mysql_query(SELECT * FROM`ingrident` WHERE`food_group_id_fk` =mysql_real_escape_string($ _ GET ['食品集团']));
    而(($数据= mysql_fetch_array($结果))!= FALSE)
        回声'<期权价值=,$数据['ingrident_id'],'>',$数据['ingrident_name'],'< /选项>';
 

我进口的名单

 <脚本SRC =htt​​ps://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js>< / SCRIPT&GT ;
&所述;脚本的src =的http://$c$c.jquery.com/jquery-2.1.1.min.js>&所述; /脚本>
 <脚本SRC =JS / bootstrap.min.js>< / SCRIPT>
<脚本SRC =JS / docs.min.js>< / SCRIPT>

<脚本SRC =JS / IE10视口-bug的workaround.js>< / SCRIPT>
 

解决方案

  $。阿贾克斯({
          网址:process.php,
          类型:'后',
          数据:{父:母},
          成功:功能(数据){
              $(#成分)的HTML(数据)。
          }
      });
 

在你的process.php

  $父= $ _ POST ['父'];
回声($父);
 

I know this question have been asked many times, but I have tried for hours and nothing worked, I'm a noob with php and ajax so, I might be missing a small thing that I'm not aware of, What I'm trying to achieve here is that, I want the user to select a food group and based on that a list of ingredients will show up.

I tested my process.php file alone and it is working well, I have also tested the script what happens is that the line starting from $.ajax doesn't work when I comment it out and type alert(parent) I actually get the value of the option selected, so what am I doing wrong here? or am I missing an import? P.S I'm using bootstrap 3.0 for the design

Here is my html code

<!--  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>

Here is my script

<script type="text/javascript">
  function ajaxfunction(parent)
  {


    $.ajax({
          url: 'process.php?food-group=' + parent;
          success: function(data) {
              $("#ingredients").html(data);
          }
      });  
  }
</script>

Here is my process.php file

<?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>';

A list of my imports

<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);
          }
      }); 

in your process.php

$parent = $_POST['parent'];
echo($parent);

这篇关于动态选择PHP和MySQL选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆