获得价值

时间:2014-10-29 12:50:53

标签: javascript php ajax

我只需要一些想法如何从下拉列表中获取价值

<select  id="ins" name="instructor" required>
    <?php
        //dropdown instructor name
        echo "<option  value=\"\">"."Select"."</option>";
        $qry = "select * from instructor";
        $result = mysqli_query($con,$qry);
        while ($row = mysqli_fetch_array($result)){
        echo "<option  value=".$row['insA_I'].">".$row['insname']."</option>";
        }
    ?>
</select>

并将此值放在$_POST['instructor']

中的.php文件中
    <?php
      session_start();
      require 'connection.php'; //connection

      $qry = "select course from instructor where insA_I = '".$_POST['instructor']."'";
      $result = mysqli_query($con,$qry);
      $_SESSION['row'] = mysqli_fetch_array($result);
      $data = $_SESSION['row']['course'];
      echo $data;
   ?>

我仍然是ajax的新手

$(document).ready(function() {
     $('#ins').change(function(){
        $.ajax({

            type: "POST",
            url: "json_php.php",
            data: '',
            datatype: 'json',
            success: function(result){
                alert(result);

            }
        });
    });
});

我已经在网上搜索了这个,但经过一些修改后没有得到我的答案 只需要想法,技术。等。

2 个答案:

答案 0 :(得分:1)

试试这个ajax代码

 $(document).ready(function() {
         $('#ins').change(function(){

            $.ajax({

                type: "POST",
                url: "json_php.php",
                data: {instructor:$(this).val()},
                success: function(result){
                    alert(result);

                }
            });
        });
    });

用于查询的php代码

$qry = "select course from instructor where insA_I = '".$_POST['instructor']."'";
      $result = mysqli_query($con,$qry);
      $fetch = mysqli_fetch_array($result);
      $_SESSION['row']['course']=$fetch['course'];
      $data = $_SESSION['row']['course'];
      echo $data;

答案 1 :(得分:0)

由于您正在使用jQuery,请使用jQuery:

data: { instructor: $('#ins').val() }

另外,请注意您的SQL查询 对SQL注入开放 This is a good place to start reading about that