douzhiling3166 2017-04-03 11:05 采纳率: 100%
浏览 101
已采纳

如何通过下拉列表中的ajax显示查询返回的数据?

I have one page where onClick of radio button I am calling one java script function which contains ajax , with the url of edit.php which has the query to return the array of chapters from table.

Now I get this chapters from database, but I want to show them in select tag of chapters. Which on first page loads has all the chapters and on click of the type(radio button) I want to show the chapters which are sorted type wise.

    <!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>MCQ Questions</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script>
        function getValue(obj) {
            var value = obj.value;
            $.ajax({
                type: "POST",
                url: 'edit.php',
                data: {
                    "val" : value
                },
                dataType: 'text',
                async: false,
                cache: false,
                success: function (result) {

                    var results = result;


                    Select chapter :
                        <select name="chapters">

                            <?php
                        if (count($results > 0)) {
                        foreach ($results as $row):?>
                        <option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?></option>
                        <?php
                        endforeach;
                        } else {
                        ?>

                        <option value="0">No data found</option>
                    <?php
                    }
                    ?>

                    // window.location.reload();
                }
            });
        }
    </script>
</head>
<body>

<?php
session_start();
//echo "type" . $_SESSION["type"] . ".<br>";

$dbh = new PDO('mysql:host=localhost;dbname=airman_handbook', 'siddhi', 'siddhi');

$type = $_SESSION["type"];
?>

<form method="post" action="uploadQuestion.php" enctype="multipart/form-data">
    <p> Enter the question :</p> <input name="question" type="text"> <br><br>
    Select question type : <br><br>

    <div id="types">

        SSgt <input name="type" type="radio" id="t2" value="1" <?= ($type == 1 ? "checked" : ""); ?>
                    onClick="getValue(this)">

        TSgt <input name="type" onClick="getValue(this)" type="radio" id="t1"
                    value="2" <?= ($type == 2 ? "checked" : ""); ?>>

        MSgt <input name="type" onClick="getValue(this)" type="radio" id="t3"
                    value="3" <?= ($type == 3 ? "checked" : ""); ?>>
    </div>

    <p> Enter options :</p>
    Enter option 1 : <input name="opt1" type="text"> <br><br>
    Enter option 2 : <input name="opt2" type="text"> <br><br>
    Enter option 3 : <input name="opt3" type="text"> <br><br>
    Enter option 4 : <input name="opt4" type="text"> <br><br>

    <p> Enter correct answer :</p>

    <input name="ans" type="input"> <br><br>
    Select chapter :
    <select name="chapters">

        <?php
        $stmt = $dbh->prepare("SELECT * FROM chapters");

        $stmt->execute();
        $results = $stmt->fetchall(PDO::FETCH_ASSOC);

        if (count($results > 0)) {
            foreach ($results as $row):?>
                <option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?></option>
                <?php
            endforeach;
        } else {
            ?>

            <option value="0">No data found</option>
            <?php
        }
        ?>

        <?php

        function getChapters($type)
        {
            $stmt = $dbh->prepare("SELECT * FROM chapters where type = $type");

            $stmt->execute();
            $results = $stmt->fetchall(PDO::FETCH_ASSOC);

            if (count($results > 0)) {
                foreach ($results as $row):?>
                    <option value="<?php echo $row['id']; ?>"><?php echo $row['title']; ?></option>
                    <?php
                endforeach;
            } else {
                ?>

                <option value="0">No data found</option>
                <?php
            }
        }

        ?>

    </select> <br><br>
    <input type="submit" value="Submit">


</form>
</body>
</html>

edit.php

    <?php


$dbh = new PDO('mysql:host=localhost;dbname=airman_handbook', 'siddhi', 'siddhi');

$stmt = $dbh->prepare("SELECT * FROM chapters where type = :type");
$stmt->bindParam("type",$_POST['val']);
$stmt->execute(); 
$results = $stmt->fetchall(PDO::FETCH_ASSOC);
foreach ($results as $row)
{
    echo $row['title'];
}
?>

I got the result in java script variable, how can I access this in php? Or any simpler way of keeping all codes separately and make it work?

Please help thank you..

  • 写回答

1条回答 默认 最新

  • dongliming2416 2017-04-03 12:24
    关注

    in edit.php, from the $results, make the formation that you want like below

    $chapters='';
    
    if (count($results > 0)) {
    
         foreach($results as $row) {
    
           $chapters.='<option value="'.$row['id'].'">'.$row['title'].'</option>';
    
         }
    
    } else {
    
      $chapters.='<option value="">No data found</option>';
    
    }
    
    echo $chapters;
    

    and in script, put the result in chapters html.

      <script>
                function getValue(obj) {
                    var value = obj.value;
                    $.ajax({
                        type: "POST",
                        url: 'edit.php',
                        data: {
                            "val" : value
                        },
                        dataType: 'text',
                        async: false,
                        cache: false,
                        success: function (result) {
    
                            var results = result;
    
                            $('#chapters').html(results);
    
    
                        }
                    });
                }
            </script>
    

    Hope this helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?