douyueju2912 2016-07-17 08:48
浏览 69

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

I have 3 select option list the first and second works fine but the third is relative with the first and second.

For example if I select the first "IT" and the second "1st year" then on the third appear the courses of IT 1st year and that i have done using ajax!

Now my problem is to make the option value selected. I have done it with the first and second dropdown list but I have problem with the third.

Below is my code

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

Code for test.php is below:

<?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>";
    }
}
?>

I've tried to make the third dropdown list option selected but it does not working. Any solution please?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)