Ajax没有从数据库中检索元素

时间:2017-11-09 00:22:54

标签: javascript php jquery ajax

我有一个下拉菜单,我填充了数据库中的项目。我想用同一个表中的其他属性填充文本字段,我从中获取下拉选项。我遇到的问题是检索其他属性,并将它们放在文本字段中。我编写了控制台日志来帮助我查看我的代码在做什么。此时,每个函数的每个控制台日志都会返回,就好像代码正常工作一样。唯一没有发生的事情是当我的代码转到ajax处理文件时,它没有获取数据,因此当它返回到主文件时,没有属性放在文本字段中。

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

    <?php                        
    # Perform database query
    $query = "SELECT * FROM student";
    $result = $conn->query($query) or die('Query 1 failed: ' . mysql_error());

    ?>

    <label for="studentSelect">Student Name: </label>
    <select id="studentSelect">
    <option value="0">Please select</option>
    <?php
    while ($row = $result->fetch_assoc())
    {
        echo '<option value="' . $row['studentID'] . '" > "' . $row['studentFirstName'] . '"  "' . $row['studentLastName'] . '"</option>';
    }
    ?>
    </select>
    <div>
        <label for="element_5_1">First Name</label>
        <input id="element_5_1" name="element_5_1" class="element text large" type="text">
    </div>

    <div>
        <span class="floatLeft">
        <label for="element_5_3">Last Name</label>
        <input id="element_5_3" name="element_5_3" class="element text medium" style="width:14em" type="text">
        </span>

        <span style="float:left">
        <label for="element_5_4">Major</label>
        <input id="element_5_4" name="element_5_4" class="element text medium" style="width:4em" type="text">
        </select>
        </span>
        <span style="float:left">
        <label for="element_5_5">Credits Earned</label>   
        <input id="element_5_5" name="element_5_5" class="element text medium" style="width:6em" type="text">
        </span>
    </div>



    <script type="text/javascript">
    function makeAjaxRequest(studentFirstName)
    {
        console.log("making request");
        $.ajax({        

        type: "POST",
        data: { studentFirstName: studentFirstName },
        dataType: "json", 
        url: "process_ajax.php",
        success: function(json) 
        {
            insertResults(json);
            console.log("successful post");
        },
        failure: function (errMsg) 
        {
            alert(errMsg);
        }
        });
        }                               

        $("#studentSelect").on("change", function()
        {       
            var id = $(this).val();
            if (id === "0")
            {
                clearForm();
                console.log("cleared form");
            }
            else 
            {
                makeAjaxRequest(id);
            }
        });                             

        function insertResults(json)
        {                                   

            $("#element_5_1").val(json["studentFirstName"]);

            $("#element_5_3").val(json["studentLastName"]);

            $("#element_5_4").val(json["major"]);

            $("#element_5_5").val(json["creditsEarned"]);
        }

        function clearForm()
        {
            console.log("in clearForm");
            $("#element_5_1, #element_5_3, #element_5_4, #element_5_5").val("");
        }

    </script>

然后我有一个单独的ajax处理文件

<?php
$host = "********.mysql.database.azure.com";
$username = "************";
$password = "*******";
$db_name = "**********";

//Establishes the connection
$conn = mysqli_init();
mysqli_real_connect($conn, $host, $username, $password, $db_name, 3306);
if (mysqli_connect_errno($conn)) {
die('Failed to connect to MySQL: '.mysqli_connect_error());

$studentName = $_POST['studentFirstName'];

$query = "SELECT * FROM student";
$result = mysql_query($query) or die('Query 2 failed: ' . mysql_error());
while ($row = mysql_fetch_assoc($result)) {
    if ($studentName == $row['studentFirstName']){
        echo json_encode($row);
    }
}
?> 

非常感谢任何帮助!

0 个答案:

没有答案