2011-03-27 97 views
0

我有以下代码:PHP和JavaScript POST

<html> 
<head> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" type="text/javascript"> 
</script> 
<script type="text/javascript"> 


function displayTweet(){ 
var i = 0; 
var limit = $("#twitter-results > div").size(); 
var myInterval = window.setInterval(function() { 
var element = $("#twitter-results div:last-child"); 
$("#twitter-results").prepend(element); 
element.fadeIn("slow"); 
i++; 
if(i==limit){ 
window.setTimeout(function() { 
clearInterval(myInterval); 
}); 
} 
},2000); 
} 

$("form#twittersearch").submit(function() { 
twitterq = $('#twitterq').attr('value'); 
$.ajax({ 
type: "POST", 
url: "search.php", 
cache: false, 
data: "twitterq="+ twitterq, 
success: function(html){ 
$("#twitter-results").html(html); 
displayTweet(); 
} 
}); 
return false; 
}); 
}); 

</script> 

</head> 
<div class="twitter_container"> 
<form id="twittersearch" method="post" action=""> 
<input name="twitterq" type="text" id="twitterq" /> 
<button type="submit">Search</button> 
</form> 
<div id="twitter-results"></div> 
</div> 
</html> 


/***************THIS IS search.php***************************/ 

<?php 
include('twitterapi.php'); 
if($_POST['twitterq']){ 
$twitter_query = $_POST['twitterq']; 
$search = new TwitterSearch($twitter_query); 
$results = $search->results(); 

foreach($results as $result){ 
    echo '<div class="twitter_status">'; 
    echo '<img src="'.$result->profile_image_url.'" class="twitter_image">'; 
    $text_n = toLink($result->text); 
    echo $text_n; 
    echo '<div class="twitter_small">'; 
    echo '<strong>From:</strong> <a href="http://www.twitter.com/'.$result->from_user.'">'.$result->from_user.'</a&glt;: '; 
    echo '<strong>at:</strong> '.$result->created_at; 
    echo '</div>'; 
    echo '</div>'; 
} 
} 
?> 

为什么,当我做了var_dump($_POST['twitterq'])它始终是NULL?

UPDATE:

萤火虫给了我这个,不知道如何虽然修复:

enter image description here

enter image description here

+0

和JS中的twitterq值?正确? – 2011-03-27 05:57:45

+0

我不认为这样做像'“twitterq =”+ twitterq,'这将工作。你有没有试过'data:{'twitterq':twitterq}'? – JohnP 2011-03-27 06:00:59

+0

尝试过,不起作用 – aherlambang 2011-03-27 06:04:37

回答

0

那是因为你没有正确地提供数据。它应该是:

data: ({twitterq: twitterq}), 

您正在以JSON的形式发送数据。你没有使用URL参数,所以不需要'='。如果你有多个参数,你可以简单地用逗号分隔它们。

+0

它仍然是NULL .... – aherlambang 2011-03-27 06:02:23

+0

我只是按照这里的教程http://www.webspeaks.in/2010/03/twitter-api-how-to-create-stream-of.html – aherlambang 2011-03-27 06:02:52

+0

'“twitterq =“+ twitterq'实际上正在工作 – 2011-03-27 06:03:38