通过ajax将多个变量值传递给php文件,运行mysql查询并检索数据

时间:2018-04-21 12:59:32

标签: php jquery html ajax

更新:我想根据用户输入的滚动号,课程和学期值从mysql表中检索学生全名。

用户在文本字段中键入卷号,同时从选择列表中选择课程和学期。当用户在输入卷号并选择课程后从选择列表中选择学期时,我想在文本字段中检索学生姓名。

我现在可以在文本框中检索学生的名字。无论如何,我可以检索学生的中间名和姓氏并将它们显示在两个不同的文本框中吗?请帮忙。

以下是我正在使用的代码:

HTML代码:

    <form id="formname1" action = "" method= "post">

  <dd>
        <table>


            <tr>
                <td>Roll No.<font color = "red"><B>*</B></font></td>
                <td><input type = "text" id = "roll_number" name = "roll_number"></td>


                <td>Course:<font color = "red"><B>*</B></font></td>
                <td>
                    <select name ="course_name" id = "course">
                        <option value = "">Select</option>
                        <option value = "B.Ed">B.Ed</option>
                        <option value = "M.Ed">M.Ed</option>
                    </select>
                </td>

                <td>Semester:<font color = "red"><B>*</B></font></td>
                <td>
                    <select name ="sem" id = "sem">
                        <option value = "">Select</option>
                        <option value = "1">1</option>
                        <option value = "2">2</option>
                        <option value = "3">3</option>
                        <option value = "4">4</option>

                    </select>
                </td>

            </tr>

            <tr>
                <td>Student Name:<font color = "red"><B>*</B></font></td>
                <td><input type = "text" id = "f_name" name = "s_name"></td>

                <td><input type = "text" id = "m_name" name = "m_name"></td>

                <td><input type = "text" id = "l_name" name = "l_name"></td>

            </tr>

        </table>

    </form>

AJX代码:

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

<script>

            $(document).ready(function () {

            $("#sem").change(function(){

             var roll_no = $("#roll_number").val(); 
             var course = $("#course").val();
             var sem = $("#sem").val();
             $.post("retrieve_name.php",{
             r_no:roll_no,cname:course,semester:sem},

             function(data){
             $("#f_name").val((data[0].first_name));
             $("#m_name").val((data[0].middle_name));
             $("#l_name").val((data[0].last_name));
             });
             }); 
        });


</script>    

PHP文件:

      <?php

        $con=mysqli_connect("$host", "$username", "$password", "$db_name");


$rows1 = array[]; 

$sql = "select first_name, middle_name, last_name from students s, students_in_courses sin
where s.roll_number = sin.roll_number
AND s.roll_number = '$roll' 
AND sin.course_name = '$course'
AND sin.semester = '$sem' ";

$result = mysqli_query($con,$sql);

if(!$result)
    {
        echo "No records matching your query were found.";  
    }
    else
    {
        while ($rows = mysqli_fetch_array($result))
        {
            //echo $rows[first_name];
            //echo $rows[middle_name];
            //echo $rows[last_name];
            $rows1[]= $rows;
        }
    }
    ?>

2 个答案:

答案 0 :(得分:0)

输入不能是内容父标记。

更改:

$("#s_name").html(data);

致:

$("#s_name").val(data);

答案 1 :(得分:0)

这是正确的脚本代码: -

$(document).ready(function () {

            $("#sem").change(function(){

             var roll_no = $("#roll_number").val(); 
             var course = $("#course").val();
             var sem = $("#sem").val();
             $.post("test.php",{
             r_no:roll_no,cname:course,semester:sem},
             function(data){
             $("#s_name").val(data);
             });
             });
 });