如何使用ajax和php选择echo选项

时间:2016-07-17 08:48:53

标签: php ajax option selected

我有3个选择选项列表,第一个和第二个工作正常,但第三个是第一个和第二个相对。

例如,如果我选择第一个" IT "第二年" 第一年"然后在第三年出现IT第一年的课程,我已经使用ajax!

现在我的问题是选择选项值。我已经用第一个和第二个下拉列表完成了它,但我遇到第三个问题。

以下是我的代码

 <select id='first' onchange="on_change()" name="dega" style="margin-left: 5px;margin-top: 5px; width:205px;background: rgba(80,80,208,103); color:#eee; text-shadow:0 1px 0 rgba(0,0,0,0.4);">
    <option style="display:none">Zgjidh Degen</option>
   <option <?php if(@$_POST['dega'] == 'Teknologji Informacioni'){ echo 'selected = \"selected\"'; } ?> value='Teknologji Informacioni'>Teknologji Informacioni</option>
   <option <?php if(@$_POST['dega'] == 'Informatike'){ echo 'selected = \"selected\"'; } ?> value='Informatike'>Informatike</option>
   <option <?php if(@$_POST['dega'] == 'Biologji'){ echo 'selected = \"selected\"'; } ?> value='Biologji'>Biologji</option>
   <option <?php if(@$_POST['dega'] == 'Matematike Fizike'){ echo 'selected = \"selected\"'; } ?> value='Matematike Fizike'>Matematike Fizike</option>
   <option <?php if(@$_POST['dega'] == 'Fizike Kompjuterike'){ echo 'selected = \"selected\"'; } ?> value='Fizike Kompjuterike'>Fizike Kompjuterike</option>
   <option <?php if(@$_POST['dega'] == 'Anglisht'){ echo 'selected = \"selected\"'; } ?> value='Anglisht'>Anglisht</option>
   <option <?php if(@$_POST['dega'] == 'Gazetari'){ echo 'selected = \"selected\"'; } ?> value='Gazetari'>Gazetari</option>
   <option <?php if(@$_POST['dega'] == 'Anglisht Gazetari'){ echo 'selected = \"selected\"'; } ?> value='Anglisht Gazetari'>Anglisht Gazetari</option>
   <option <?php if(@$_POST['dega'] == 'Italisht Gjermanisht'){ echo 'selected = \"selected\"'; } ?> value='Italisht Gjermanisht'>Italisht Gjermanisht</option>
</select>
<select id='second' class="chosen-select" name="viti" onchange="on_change()" style="margin-left: 5px;margin-top: 5px; width:205px;background: rgba(80,80,208,103); color:#eee; text-shadow:0 1px 0 rgba(0,0,0,0.4);">
   <option style="display:none" >Zgjidh Vitin</option>
   <option <?php if(@$_POST['viti'] == 'Viti i pare'){ echo 'selected = \"selected\"'; } ?> value='Viti i pare'>Viti i pare</option>
   <option <?php if(@$_POST['viti'] == 'Viti i dyte'){ echo 'selected = \"selected\"'; } ?> value='Viti i dyte'>Viti i dyte</option>
   <option <?php if(@$_POST['viti'] == 'Viti i trete'){ echo 'selected = \"selected\"'; } ?> value='Viti i trete'>Viti i trete</option>
   <option <?php if(@$_POST['viti'] == 'Viti i katert'){ echo 'selected = \"selected\"'; } ?> value='Viti i katert'>Viti i katert</option>
</select>
<select id='third'  name="lenda" style="margin-left: 5px;margin-top: 5px; width:205px;background: rgba(80,80,208,103); color:#eee; text-shadow:0 1px 0 rgba(0,0,0,0.4);">
   <option style="display:none" >Zgjidh Lenden</option>
   <option id="third"></option>

     <script   src="js/jquery-2.2.4.min.js"></script>
<script type="text/javascript">
    function on_change()
    {
        var f=$("#first").val();
        var s=$("#second").val();
        if(f!="" && s !="")
        {
            $.ajax({
                type:"POST",
                url:"test.php",
                data:{ "first":f,"second":s},
                success:function(res)
                {
                    $("#third").html(res);  
                }
            });
        }
    }
</script>


</select>

test.php 的代码如下:

<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "aplikacioni";
$conn = mysqli_connect("localhost", "root", "", "aplikacioni");
if ($_POST['first']) {
    $str = $_POST['first'];
    $str2 = $_POST['second'];
    $q = "select emri from lendet where dega='$str' AND viti_shkollor='$str2' ";
    $res = mysqli_query($conn, $q);
    $flag = "0";
    while ($row = mysqli_fetch_assoc($res)) {
        $flag++;
        $emrin = $row['emri'];
        echo "<option " . ((@$_POST['lenda'] == $emrin) ? 'selected="selected"' : "") . " value=" . $emrin . ">" . $emrin . "</option>";
    }
    if ($flag == 0) {
        echo "<option value=''>Nuk ka lende </option>";
    }
}
?>

我已尝试选择第三个下拉列表选项,但它无效。有什么解决方案吗?

0 个答案:

没有答案