如何将GET参数传递给ajax并在弹出窗口中加载数据?

时间:2015-05-29 00:32:47

标签: php jquery ajax

我有一个html页面,其中包含一个包含课程链接的表格。当您将鼠标悬停在链接上时,数据库中课程表中的信息应显示在引导程序弹出框或信息框内,并调用ajax以便异步加载信息。我使用$(this).attr('href')来拉取页面的内容。但是,它会提取页面的全部内容,而我只想要div中的信息。这是我的PHP脚本的内容:

<?

// Display table for IT plan
$submitted = isset($_POST['submit']);

// when form is submitted, include the correct schedule
if ($submitted) {

    $prog = $_POST['program'];
    $qtr = $_POST['quarter'];
    $class = $_POST['class'];

    // if bas program is selected, modify the sched string
    if ($prog == "bas-network" || $prog == "bas-software") {
        $sched = $prog. '-' .$qtr. '-' .$class;
        //echo $sched;
    } else {
        $sched = $prog. '-' .$qtr;
    }

    $path = "schedules/";
    include ($path . $sched .'.html');
}

require 'connect.php';

$id = $_GET['courseid'];
//print_r($_GET);

if ($id) {
    try {
        // Create a new PDO connection object
        $dbh = new PDO('mysql:host=localhost;dbname=jim_grcc', $user, $pass);
        // Set the error mode
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $dbh->prepare('SELECT * FROM course WHERE courseid = :id');
        $stmt->execute(array('id' => $id));
        // Get array containing all of the result rows
        $row = $stmt->fetch();
        // Print data if one of the rows were returned
        if ($row != null  ) {
            echo "<div>";
            echo "<h3>".$row['coursenum']. ": " .$row['title'] . "</h3>";
            echo "<strong>Course number: </strong>" .$row['coursenum'] ."<br><br>";
            echo "<strong>Title: </strong>" .$row['title'] ."<br><br>";
            echo "<strong>Description: </strong>" .$row['description'] ."<br><br>";
            echo "<strong>Credits: </strong>" .$row['credits'] ."<br><br>";
            echo "<strong>Prerequisites:</strong> " .$row['prereqs'] ."";
            echo '</div>';
        } else {
            echo "No rows returned";
        }
    }
    catch(PDOException $e)
        {
            echo 'Error: ' . $e->getMessage();
        }
}
?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script>
    $(document).ready(function() {
        $("table a").hover(function() { 
            var href = $(this).attr('href');
            $.get(href, function( data ) {
                alert(data);
        }); 
    });
});

</script>

以下是表格的设置方式:

<tr>
    <td><a href="../it-advising/programs-process.php?courseid=it114">IT 114</a></td>
    <td><a href="../it-advising/programs-process.php?courseid=it131">IT 131</a></td>
    <td><a href="../it-advising/programs-process.php?courseid=it135">IT 135</a></td>
    <td><a href="../it-advising/programs-process.php?courseid=it190">IT 190</a></td>
</tr>

<tr>
    <td><a href="../it-advising/programs-process.php?courseid=it141">IT 141</a></td>
    <td><a href="../it-advising/programs-process.php?courseid=it160">IT 160</a></td>
    <td><a href="../it-advising/programs-process.php?courseid=it201">IT 201</a></td>
    <td>&nbsp;</td>
</tr>

我正在使用GET参数,因为courseid是通过链接传递的。查询字符串的示例是:programs-process.php?courseid = it190。我以为我可以在$ .get中传递id作为数据参数,但是控制台显示为null或未定义。

1 个答案:

答案 0 :(得分:0)

有一个单独的php文件,只根据传递给它的值发送这些信息。您将标识符变量发送到文件,并让它返回您需要的数据。

您获取整个页面的原因很可能是您正在使用的文件中有HMTL标记。