使用Ajax将值发布到PHP页面

时间:2013-11-30 22:15:19

标签: php jquery ajax

下面是一个包含HTML和jQuery代码的文件,只要释放输入文本框的其中一个键,就会将值发布到另一个页面(Student Search Results.php)。

<html>
<head>

    <script src="JQuery.js"></script>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Team Search</title>



</head>
<body>

    <form action="Student Search Results.php" method="Post">

    <center>Team Name: <input type="text" name="TeamName" class="search" id="TeamName">  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;              
    Teacher Sponsor: <input type="text" name="Teacher" class="search" id="Teacher"></center><hr>

    </form>

        <script>

$(".search").keyup(function(){

   var Team_Name = $('#TeamName').val();
   var Teacher = $('#Teacher').val();

   var Search_Data = Team_Name + '?????' + Teacher;

   $.ajax({
type: "POST",
url: "Student Search Results.php",
data: {query: Search_Data},
cache: false,
success: function(){
alert('The values were sent');
}
});

});

</script>  


    </body>
</html>

以下是学生搜索结果.php页面:

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>

    <?php

    include "Connection.php";

        $searchData = explode('?????', $_POST['Search_Data']);

        $teamName = $searchData[0];
        $teacher = $searchData[1];

        $query = "SELECT club_table.Club_Name, teacher_user_table.Teacher_Name
            WHERE club_table.Teacher_Email = teacher_user_table.Teacher Email,
            teacher_user_table.Teacher_Name LIKE '%" . $teacher . "%', 
            club_table.Club_Name LIKE '%" . $teamName . "%';";

        mysqli_query($con, $query);

        echo $query;

    ?>

</body>

但是,$ _POST ['Search_Data']被视为未定义。

对于为什么会出现这种情况有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

$_POST['XXX']中括号中的名称必须是已过帐值的名称,例如query

所以你可以:

将POST data中的值名称从query更改为Search_Data

...
data: {Search_Data: Search_Data},
...

或更改PHP中的索引名称:

$searchData = explode('?????', $_POST['query']);