PHP MYSQL动态选择框 [英] PHP MYSQL dynamic select box

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

问题描述

我想创建一个搜索框,从框1选择的选项填充可用于BOX2的选项。这两个框的选项是从我的MYSQL数据库给出。我的问题是,我不知道如何执行基于第一查询的查询,而无需刷新这将是乏味和恼人的页面。

I am trying to create a search box where the option selected from 'box1' populates the options available for 'box2'. The options for both boxes are given from my MYSQL database. My problem is that I do not know how to perform a query based on the first query without refreshing the page which would be tedious and annoying.

HTML / PHP

HTML / PHP

<form role="form" action="search.php" method="GET">
          <div class="col-md-3">
              <select class="form-control">
                  <?php
                  $result = mysqli_query($con,"SELECT `name` FROM school");
                  while($row = mysqli_fetch_array($result)) {
                    echo '<option name="'.$row['name'].'">'.$row['name'].' School</option>';
                  }

                  ?>
              </select>
          </div>
          <div class="col-md-3">
              <select class="form-control">
                  <?php
                  $result = mysqli_query($con,"SELECT * FROM products");
                  while($row = mysqli_fetch_array($result)) {
                    echo '<option name="'.$row['product'].'">'.$row['product'].'</option>';
                  }
                  mysqli_close($con);
                  ?>
              </select>
          </div>
          <button type="submit" class="btn btn-info">Search</button>
    </form>

我想查询会去这样的事情。 AJAX可以解决这个问题,但我不能确定如何使用AJAX无刷新执行此查询。

I think that the query would go something like this. AJAX may be the solution to this problem but I am unsure how to use AJAX to perform this query without refreshing.

SELECT `product` FROM products WHERE `school` = [SCHOOL NAME FROM BOX 1]

在此先感谢!

Thanks in advance!

推荐答案

首先创建NO1选择菜单用PHP,你上面提到的。然后一个'变'事件监听添加到它,如:

First create the no1 select menu with php as you mentioned above. Then add a 'change' eventListener to it like:

$('#select1').change(createSelect2);

function createSelect2(){
    var option = $(this).find(':selected').val(),
    dataString = "option="+option;
    if(option != '')
    {
        $.ajax({
            type     : 'GET',
            url      : 'http://www.mitilini-trans.gr/demo/test.php',
            data     : dataString,
            dataType : 'JSON',
            cache: false,
            success  : function(data) {            
                var output = '<option value="">Select Sth</option>';

                $.each(data.data, function(i,s){
                    var newOption = s;

                    output += '<option value="' + newOption + '">' + newOption + '</option>';
                });

                $('#select2').empty().append(output);
            },
            error: function(){
                console.log("Ajax failed");
            }
        }); 
    }
    else
    {
        console.log("You have to select at least sth");
    }
}

现在根据选择1选择的选项,NO2选择菜单中有新的选择。

Now the no2 select menu has new options according to the select 1 selected option.

和PHP文件:

<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

if(isset($_GET['option']))
{
    $option = $_GET['option'];

    if($option == 1)
    {
        $data = array('Arsenal', 'Chelsea', 'Liverpool');
    }
    if($option == 2)
    {
        $data = array('Bayern', 'Dortmund', 'Gladbach');
    }       
    if($option == 3)
    {
        $data = array('Aek', 'Panathinaikos', 'Olympiakos');
    }

    $reply = array('data' => $data, 'error' => false);
}
else
{
    $reply = array('error' => true);
}

$json = json_encode($reply);    
echo $json; 
?>

当然有,我使用了一些演示数据,但你可以让一个SQL查询填充$ data数组,并且把它们发送的JSON用正确的头。最后,多用一些JS第二选择菜单:

Of course there i use some demo data but you can make an sql query populate the $data array there and send them as json with the right headers. Finally use some more js for the second select menu:

$('#select2').change(selectSelect2);

function selectSelect2(){
    var option = $(this).find(':selected').val();
    if(option != '')
    {
        alert("You selected: "+option);
    }
    else
    {
        alert("You have to select at least sth");
    }
}

这里检查 http://jsfiddle.net/g3Yqq/2/ 的工作示例

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

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