0

単純なフォームを使用してハッシュタグの名前をphpファイルに設定していますが、結果をフォームと同じページに表示したいと思います。私のフォームとPHPは次のとおりです

    <form id"search" method="post" action="twitter.php">
    <input type="text" name="hash" id="hash"/>
    <input type="submit" name="submit" value="submit"/>
    </form>   

私のPHPはハッシュタグ検索の結果を表示します

    <?php
    global $total, $hashtag;
    $hashtag = $_POST["hash"];
    $total = 0;
    function getTweets($hash_tag, $page) {
    global $total, $hashtag;
    $url = 'http://search.twitter.com/search.json?q='.urlencode($hash_tag).'&';
    $url .= 'page='.$page;    
    $ch = curl_init($url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $json = curl_exec ($ch);
    curl_close ($ch);
    echo "<pre>";    
    $json_decode = json_decode($json);
    print_r($json_decode->results);

    $json_decode = json_decode($json);        
    $total += count($json_decode->results);    
    if($json_decode->next_page){
     $temp = explode("&",$json_decode->next_page);        
     $p = explode("=",$temp[0]);                
     getTweets($hashtag,$p[1]);
      }        
    }
  echo $total;
  getTweets($hashtag,1);
    ?>

phpの結果をフォームの下と同じページに表示するにはどうすればよいですか。よろしくお願いします。

4

1 に答える 1

0

同じページに投稿します。フォーム「twitter.php」でアクションを定義します。これは、フォームが投稿情報を含むページ twitter.php に移動することを意味します。

同じページに投稿する場合は、そこで投稿リクエストをキャッチし、リクエストされたものをフォームの下または上に表示します。両方の要素がページに表示されます。

小さな例:

<?php
if ($_SERVER['REQUEST_METHOD'] == "POST" && !empty($_POST['hashtag'])) {
    echo "You searched for: " . $_POST['hashtag'] . "!";
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    Hashtag: <input type="text" name="hashtag" /> <br />
    <input type="submit" value="Submit hashtag!" />
</form>

これにより、ハッシュタグの入力フィールドを含むフォームが表示されます。スクリプトが同じページに投稿されると、PHP は POST 要求をキャッチし、検索されたものを表示します。代わりに、この値を使用して、Twitter 自体から検索アクションを取得できます。

于 2012-10-15T11:16:28.430 に答える