0

这是我项目中的 PHP 文件。我使用 href 来获取“获取电影详细信息”,因为它会转到另一个页面并显示结果。

<a href = "movie_details.php?movie_id=$movie_id" ... 

我需要使用 ajax 请求获取这些详细信息并在同一页面中呈现响应(如果它在 div 内部更好)。

表1.php

<?php 
    $link = mysql_connect("localhost","root","") or die(mysql_error());

    mysql_selectdb("MovieSite") or die(mysql_error());

    $query = "SELECT movie_id, movie_name, movie_director, movie_leadactor FROM Movie ";
            //"WHERE movie_year > 1990 ORDER BY movie_type";

    $result = mysql_query($query, $link) or die(mysql_error());

    $num_movies = mysql_num_rows($result);

$movie_header = <<<EOD
        <h2><center>Movie Review Database</center></h2>
        <table width = "70%" border = "1" cellpadding = "2" cellspacing = "2" align = "center">
            <tr>
                <th>Movie Title</th>
                <th>Movie Director </th>
                <th>Movie Lead Actor</th>
            </tr>       
EOD;

function get_director(){
    global $movie_director;
    global $director;

    $query_d = "SELECT people_fullname FROM people WHERE people_id = '$movie_director'";

    $results_d = mysql_query($query_d) or die(mysql_error());

    $row_d = mysql_fetch_array($results_d);

    extract($row_d);

    $director = $people_fullname;

}

function get_leaderactor(){
    global $movie_leadactor;
    global $leadactor;
    $query_l = "SELECT people_fullname FROM people WHERE people_id = '$movie_leadactor'";

    $result_l = mysql_query($query_l);

    $row_l = mysql_fetch_array($result_l);
    extract($row_l);
    $leadactor = $people_fullname;
}

$movie_details = '';

while ($row = mysql_fetch_array($result)){
    $movie_id = $row['movie_id'];
    $movie_name = $row['movie_name'];
    $movie_director = $row['movie_director'];
    $movie_leadactor = $row['movie_leadactor'];

    get_director();
    get_leaderactor();

$movie_details .= <<<EOD
    <tr>
        <td><a href = "movie_details.php?movie_id=$movie_id" title = "Find out more about $movie_name">$movie_name</a></td>
        <td>$director</td>
        <td>$leadactor</td>     
    </tr>
EOD;
}

$movie_details .= <<<EOD

    <tr>
        <td>Total : $num_movies Movies </td>
    </tr>
EOD;

$movie_footer = "</table>";

$movie = <<< MOVIE
    $movie_header;
    $movie_details;
    $movie_footer;
MOVIE;
echo $movie;
?>

movie_details.php

<?php
    $link = mysql_connect("localhost", "root", "") or die(mysql_error());

    mysql_select_db('MovieSite') or die("DB Select" . mysql_error());

    function calculate_differences($takings, $cost){
        $difference = $takings - $cost;

        if ($difference < 0){
            $difference = substr($difference, 1);
            $font_color = 'red';
            $profit_or_loss = "Rs. " . $difference . "M";           
        }elseif ($difference > 0){
            $font_color = 'green';
            $profit_or_loss = "Rs. " . $difference . "M";
        }else {
            $font_color = 'blue';
            $profit_or_loss = "Broke even";
        }
        return "<font color = \"$font_color\"> ". $profit_or_loss . "</font>"; 
    }

    function get_director(){
        global $movie_director;
        global $director;

        $query_d = "SELECT people_fullname FROM people WHERE people_id = '$movie_director'";

        $results_d = mysql_query($query_d) or die(mysql_error());

        $row_d = mysql_fetch_array($results_d);

        extract($row_d);

        $director = $people_fullname;

    }

    function get_leaderactor(){
        global $movie_leadactor;
        global $leadactor;
        $query_l = "SELECT people_fullname FROM people WHERE people_id = '$movie_leadactor'";

        $result_l = mysql_query($query_l);

        $row_l = mysql_fetch_array($result_l);
        extract($row_l);
        $leadactor = $people_fullname;
    }

    $movie_query = "SELECT * FROM Movie WHERE movie_id = '" . $_GET['movie_id'] . "'";

    $result = mysql_query($movie_query, $link) or die(mysql_error());

$movie_header = <<<EOD
    <h2><center>Movie Review Database</center></h2>

EOD;

while ($row = mysql_fetch_array($result)){
    //$movie_id = $row['movie_id'];
    $movie_name = $row['movie_name'];
    $movie_director = $row['movie_director'];
    $movie_leadactor = $row['movie_leadactor'];
    $movie_year = $row['movie_year'];
    $movie_running_time = $row['movie_running_time'] . "Mins";
    $movie_takings = $row['movie_taking'];
    $movie_cost = $row['movie_cost'];

    get_director();
    get_leaderactor();
}

$movie_health = calculate_differences($movie_takings, $movie_cost);

$page_start = <<<EOD
<html>
    <head>
        <title>Details and Review for: $movie_name </title>
    </head>
    <body>
EOD;

$movie_table_headings =<<<EOD
<tr>
    <th>Movie Title</th>
    <th>Year of Release</th>
    <th>Movie Director </th>
    <th>Movie Lead Actor</th>
    <th>Movie running Time</th>
    <th>Movie Health</th>
</tr>
EOD;

$movie_details = <<<EOD
<table width = "70%" border = "1" cellpadding = "2" cellspacing = "2" align = "center">
    <tr>
        <th colspan = "6"><u><h2>$movie_name: details</h2></u></th>
    </tr>
    $movie_table_headings   
    <tr>
        <td width = "33%" align = "center">$movie_name</td>
        <td align = "center">$movie_year</td>
        <td align = "center">$director</td>
        <td align = "center">$leadactor</td>
        <td align = "center">$movie_running_time</td>
        <td align = "center">$movie_health</td>
    </tr>
</table>
<br /><br />
EOD;

$page_end = <<<EOD
    <a href = "table1.php">Home</a>
    </body>
</html>
EOD;

$movie = <<< MOVIE
    $page_start;
    $movie_details;
    $page_end;
MOVIE;
echo $movie;
mysql_close();
4

1 回答 1

1

AJAX 的工作原理是允许您使用客户端代码(例如,javascript/jQuery)检索页面的内容,而无需让用户离开当前页面。

当您发出 ajax 请求时,您可以指定要通过 GET 或 POST 发送的参数(或根本不发送任何参数)。然后,您的代码将收到页面的内容,就好像您使用浏览器访问过它一样(如果您转到该页面,右键单击并查看源代码,您会得到什么)。

所以你想要用你的 PHP 做的是在你的当前页面上输出你需要的值。通常的做法是使用 JSON 或 XML(即 ajax 中的 X)对这些数据进行编码,以便您的客户端代码可以更轻松地读取它。(我个人更喜欢 JSON)。

学习 jQuery:http

://w3schools.com/jquery/default.asp 了解 AJAX:http

://w3schools.com/ajax/default.asp 了解使用 jQuery 的 Ajax:http: //api.jquery.com/jQuery .ajax/
http://api.jquery.com/jQuery.get/
http://api.jquery.com/jQuery.post/

让我知道这是否回答了您的问题..

于 2013-04-14T03:44:46.617 回答