如何使用AJAX

时间:2017-03-01 14:06:32

标签: javascript php jquery ajax

我有这段代码来获取IMDB电影信息 我有一个网站http://microdownload.ir可以下载免费电影。

有关一系列电影的信息,我需要使用IMDB API,我使用下面的代码,但它刷新我的页面并在完成之前发布我的文章,所以我需要获取Ajax的信息我使用下面的代码

<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Get IMDB Info</title>
</head>

<body>
  <center>
    <form method="post">
      Address : <input type="text" name="url" size="50"   placeholder="Example: http://www.imdb.com/title/tt1340138" dir="ltr"><br><br>
      <input type="submit" value="Get">
    </form>
  </center>
  <br><br>
  <?php 
    error_reporting(E_ERROR | E_PARSE);
    if(empty(!$_POST['url'])){
        echo '<div dir="ltr">';
        function IMDB ($url){
            preg_match("/tt\\d{7}/i", $url, $Id);
            $Get = file_get_contents('http://www.omdbapi.com/?i='.$Id[0]);
            $json = json_decode($Get,true);
            return $json;
        }

        $Response = IMDB($_POST['url']);
        if(!$Response){
            die ('خطا!');
        }
        echo "Title: ".$Response['Title'].'<br>'.
        "Year: ".$Response['Year'].'<br>'.
        "Rated: ".$Response['Rated'].'<br>'.
        "Released: ".$Response['Released'].'<br>'.
        "Runtime: ".$Response['Runtime'].'<br>'.
        "Genre: ".$Response['Genre'].'<br>'.
        "Director: ".$Response['Director'].'<br>'.
        "Writer: ".$Response['Writer'].'<br>'.
        "Actors: ".$Response['Actors'].'<br>'.
        "Plot: ".$Response['Plot'].'<br>'.
        "Language: ".$Response['Language'].'<br>'.
        "Country: ".$Response['Country'].'<br>'.
        "Awards: ".$Response['Awards'].'<br>'.
        "Poster: ".$Response['Poster'].'<br>'.
        "Metascore: ".$Response['Metascore'].'<br>'.
        "imdbRating: ".$Response['imdbRating'].'<br>'.
        "imdbVotes: ".$Response['imdbVotes'].'<br>'.
        "imdbID: ".$Response['imdbID'].'<br>'.
        "Type: ".$Response['Type'].'<br>'.
        "Response: ".$Response['Response'].'<br>'.
        '</div>'
        ;


    }
    ?>
</body>

</html>

它的工作正常但是 我想通过AJAX(没有重新加载页面)将数据接收到我的<p></p>标签 这意味着当我点击获取数据时,我的数据设置为<p></p>标签自动

1 个答案:

答案 0 :(得分:1)

请你试试这个: -

<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>Get IMDB Info</title>
</head>

<body>
  <center>
    <form id="imdbInfoForm" method="post" >
      Address : <input type="text" name="url" size="50"   placeholder="Example: http://www.imdb.com/title/tt1340138" dir="ltr"><br><br>
      <input type="submit" value="Get">
    </form>
  </center>
   <div id="result">
        </div>
 
</body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
 
</html>
File