Ajax Post和检索多个变量

时间:2018-09-13 09:58:58

标签: javascript php html arrays ajax

我需要有关如何使用Ajax Post发布和检索多个变量的帮助。我实际上需要检索发布的变量以进行SQL查询。请参阅下面的Ajax代码,其中我需要在帖子中添加变量名selschool,selprogram和selsession

<script>

    $("#session").change(function()
    {
        $("#loding2").show();
        var id=$(this).val();
        var dataString = 'id='+ id;
        var selschool=document.getElementById("selectedschool").val();
        var selprogram=document.getElementById("selectedprogram").val();
        var selsession=document.getElementById("selectedsession").val();

        $("#semester").find('option').remove();
        $("#class").find('option').remove();
        document.getElementById("selectedclass").value= " ";
        document.getElementById("selectedsemester").value= " ";
        $.ajax
        ({
            type: "POST",
            url: "get_class.php",
            data: dataString,
            cache: false,
            success: function(html)
            {
                $("#loding2").hide();
                $("#class").html(html);
            }
        });
    });

</script>

也请参见下面的PHP脚本,我要在其中使用发布的变量进行查询;

 <?php
include('dbconfig.php');
if($_POST['id'])
{
    $id=$_POST['id'];

//  Todo: I actually needed something like where session SELECT * FROM class where session_id=$id and program_id="selprogram" and school_id="selschool"
    $stmt = $DB_con->prepare("SELECT * FROM class where session_id=$id ");
    $stmt->execute(array(':id' => $id));

    ?><option selected="selected">Select Class :</option>
    <?php while($row=$stmt->fetch(PDO::FETCH_ASSOC))
    {
        ?>
        <option value="<?php echo $row['class_id']; ?>"><?php echo $row['class_name']; ?></option>
        <?php
    }
}
?>

0 个答案:

没有答案