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

如何通过下拉列表中的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 :)

    打赏 评论

相关推荐 更多相似问题