使用AJAX发送数据

时间:2015-09-19 16:37:59

标签: php jquery ajax

这一定是这里最常被问到的问题,但我现在已经摸不着头脑了一个小时,我无法弄清楚为什么我的数据无法进入服务器端。

我的HTML,我有一个简单的表,我的目标是让表中的所有用户和每一行都有一个链接,重定向到另一个PHP页面,该页面应该根据行的特定用户名查询一些数据对应于:

<?php
include_once 'includes/psl-config.php';
include_once 'includes/db_connect.php';
include 'includes/functions.php';
?>

<!DOCTYPE html>
<html>
<head>
    <title>Individual Data</title>
    <script src="js/jquery-2.1.3.min.js"></script>
    <script src="js/jquery-ui-1.11.3.min.js"></script>
    <script src="js/ind.js"></script>
</head>
<body>
<table id="highscore">
    <tr id='title'>
        <th></th>
        <th>Username</th>
        <th>Trials</th>
        <th>RDM</th>
    </tr>

    <?php
        $scores = getTopScores($mysqli);
        $nr = 1;
        foreach($scores as $row){
            echo '<tr id="highscore"><td id="id">'.$nr.'.'.'</td><td id="username">'.$row['username'].'</td><td>'.$row['trial'].'</td>'.'<td><a id="irdm" href="individualRDM.php">RDM</a></td></tr>';
            $nr+=1;
        }
    ?>
</table>

</body>
</html>

这是我发送数据的JavaScript:

$(window).load(function(){
    $('a#irdm').click(function(){
        var $item = $(this).closest("tr")
                       .find("#username")
                       .text(); 

    $.ajax({
        type: 'post',
        url: 'individualRDM.php',
        data: {
            username: "test"
        },
        success: function( data ) {
            console.log( data );
        }
    });


    });
});

这是我希望看到数据的PHP文件:

<?php

include_once 'includes/psl-config.php';
include_once 'includes/db_connect.php';

var_dump($_POST);

if(isset($_POST['username']))
{
    $username = $_POST['username'];

    print $username;
}
else 
{
  echo "No username supplied";
}
?>

现在的问题是$ _POST变量是完全空的。我很抱歉,如果这可能与其他所有与Ajax相关的问题都是重复的,但是我已经经历了很多这样的问题而我仍然无法解决它。一些帮助将非常感激。

0 个答案:

没有答案
相关问题