在< select>中更改值使用另一个< select>使用AJAX(PHP) [英] Changing the values in a <select> using another <select> using AJAX (PHP)

查看:84
本文介绍了在< select>中更改值使用另一个< select>使用AJAX(PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用另一个选择来更改选择的值

I'm trying to change the values of a select using another select

如您所见,两个选择都使用PHP填充.

As you can see both selects are filled using PHP.

现在我想要的是,选择有一定特长的情况下,选择包含这些医师会改变.

Now what I wanted was that when choosing a certain specialty, the select containing the medics would change.

这是代码

            <select id="specialties" class = "formulario_text">
                <?php foreach ($specialties as $especialidade_id => $especialidade) { ?>
                    <option value="<?php echo $especialidade_id; ?>"><?php echo $especialidade; ?></option>
                    <?php
                }
                ?>
            </select>
            <br>
            <br>
            <label>
                <b class="formulario_labels2">Médico:*</b>
            </label>
            <br>
            <select id="medics" class="formulario_text">
                <?php foreach ($medics as $id => $nome) { ?>
                    <option value="<?php echo $id; ?>"><?php echo $nome; ?></option>
                    <?php
                }
                ?>
            </select>

我对AJAX,PHP和Javascript知之甚少,所以我在这里遇到了很多麻烦.希望有人能帮助我.

I have little knowledge on AJAX, PHP and Javascript, so I'm pretty much hitting a wall here. Was hoping anyone can help me.

预先感谢

到目前为止,我有这个

    $(document).ready(function() {


        $("#specialties").change(function() {
            var val = $(this).val();
            if (val == "16") {
                $("#medics").html("<option value='1'> <?php $medics[1]; ?> </option>");
            } 
        });

    });

我正在测试是否可以捕获另一个文件中$ medics数组的第一个位置.

I was testing to see if I could capture the first position of the $medics array that I have in another file.

$ specialties = model \ ClinicalSpecialty :: getSpecialties();

$specialties = model\ClinicalSpecialty::getSpecialties();

$ medics = model \ Medic :: getMedics();

$medics = model\Medic::getMedics();

方法像这样

static function getSpecialties(){

    $res = array();
    $db = new DBAccess();

    if($db->conn){
        $query = 'select id,name from clinical_specialty';
        $result_set = $db->conn->query($query);
        while ($row = $result_set->fetch_assoc()) {
            $res[$row['id']] = $row['name'];
        }
        $result_set->free();
        $db->conn->close();
    }
    return $res;
}

很抱歉一开始没有添加此代码

Sorry for not adding this code at first

推荐答案

最好将其分为几步,这是(我认为)最容易理解的方法.

You would be best off dividing this in steps, which is (I think) the easiest way to understand it.

我不确定如何将其包含在PHP中,因为我对其余代码一无所知.我将给您一个静态示例,您将不得不自己将其与PHP结合使用.我希望无论如何都会有帮助!

I do not know for sure how to include this with your PHP, as I don't know anything about the rest of your code. I will give you a static example and you will have to combine it with the PHP yourself. I hope it'll help anyways!

第1步:包括jquery.

您必须在html <head>标记之间包含jQuery库,如下所示: (如果您已经包含jQuery,请跳过此步骤)

You have to include the jQuery library between your html <head> tags like so: (if you already have jQuery included, skip this step)

<!--including jQuery here -->
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>

第2步:使用javascript. 首先,您必须触发onload回调,以便您的函数知道何时关闭,就像这样:

step 2:working in javascript. You first have to fire an onload callback so your functions know when to go off, like so:

<script>
//When page is done loading
$(document).ready(function(){
});
</script>

第3步:大脚本

//When page is done loading
$(document).ready(function() {
// let's get your select first!
    var firstSelector = $('#yourSelectID');
    var medicSelector = $('#yourMedicSelectID');
    //we will fill this variable on select changes
    var currentSelection = firstSelector.val();
    //set initial medic values
    medicSelector.html(
    "your options here as strings (between the quotes). see below for example"

);


    //get value on change
    firstSelector.change(function() {
        currentSelection = $(this).val();

        //check if the current value is what you want
        if (currentSelection == "1") {
            //This replaces the options completely
            medicSelector.html(
                    //you can use any PHP variable here to fill it. 
                    //Just use <?php echo $optionsforthischoice ?>
                    // $optionsforthischoice would be filled like so:
                    //  $optionsforthischoice = "<option>option1</option><option>option2</option> etc.;
                    "<option>option1</option><option>option2</option><option>option3</option>"
                    );
        }
        if (currentSelection == "2") {
            // do the same here as for 1, but when 2 is selected!
        }

    });
});

重要说明: 如您所见,这种样式非常静态.如果您想要一个更动态的解决方案,您将为我们提供更好的信息和更多的代码.

Important note: This style is quite static, as you can see. If you want a more dynamic solution you are going to give us better info and more code.

此Javascript替换了您以前使用的选项,因此您必须为每个if语句重写它们.它们不加或减.它们完全取代了.

This Javascript replaces the options you had previously, so you have to rewrite them for every if statement. They do not add or substract. They completely replace.

!编辑!

我看到您已经编辑了问题.现在,我可以适当地提供帮助.我将上面的教程"留作参考. (顺便说一句,您也可以使用php foreach循环将完整数组也添加到您的javascript中)

I see you have edited your question. Now I can properly help. I will leave the above "tutorial" for reference. (By the way, you can use a php foreach loop te get the full array into your javascript as well)

请参见下文

如果要正确执行此操作,则可能需要连接数据库中的点,并改用sql joins.这样可以节省大量代码,并使整个过程变得更容易.但是,由于您可能还没有,因此只有一种适当的方法可以解决此问题. 您会看到,无法在特定功能上使用ajax.您将必须制作一个php脚本.

If you want to do this properly, you might want to connect the dots in the database, and use sql joins instead. It would save you a lot of code and make the whole thing easier. However, since you probably haven't, there is only one proper way to solve this. You see, you cannot use ajax on a specific function. You will have to make a php script.

例如,您将创建"getMedics.php".

You will make for example "getMedics.php".

然后,你将调用getMedics.php"文件与AJAX调用,当所述选择的变化,像这样:

Then you will call the "getMedics.php" file with an ajax call when the selection changes, like so:

//The first option is the link to the file
//The ?specialty is a parameter which you are going to get in the script.
//This will be the selection that was made before
//Within the function "result" will be the variable with the values the phpscript returns
$.get('phpscripts/getMedics.php?specialty=' + $("#specialties").val(), function(result) {
//if the result returned anything
    if (result.trim()) {
        //you will want the result to be the options
        $('#medics').html(result);
    }
});

现在php脚本将更加棘手.这是关于它的外观:

Now the php script will be trickier. This is about how it's going to look:

//get the specialty ID
$specialty = strip_tags($_GET['specialty']);
//get the medics
//i'm guessing the results will be arrays within arrays
$medics = Medic::getMedics();
//set the array for the options
$options = array();
//prepare our results
$optionResults = "";

switch ($specialty) {
    case 0:
        $options[1] = "1";
        $options[2] = "2";
        $options[3] = "5";
        $options[4] = "7";
        break;
    case 1:
        $options[1] = "1";
        $options[2] = "2";
        $options[3] = "5";
        $options[4] = "7";
        break;
    case 2:
        echo "specialty equals 2";
        break;
}

//this foreach loop will pass the options to a variable
foreach($option as $options){
    //.= adds to the variable
    $optionResults .= "<option value='".$medics[$option][0]."'>".$medics[$option][1]."</option>";
}

//this is what will return as the ajaxed results:

echo $optionResults;

这篇关于在&lt; select&gt;中更改值使用另一个&lt; select&gt;使用AJAX(PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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