我的 html 表单上的输入有问题。目前,我正在将 twitter 名称的输入输入到 Ajax 函数中,该函数通过 php 从 twitter api 调用推文。
问题是,如果输入为空或输入不是有效的推特帐户,它会执行对推特 api 的 php 调用,并返回一条错误消息。
如果 twitter 帐户不存在或输入为空,如何停止 Ajax 运行?php端会发生什么事情吗?
这是输入的html:
<div id="topdiv">Input Twitter ID:
<input type="text" id="userid" onkeydown="if(event.keyCode===13) {document.getElementById('tweet-button').click();}">
<input type="submit" id="tweet-button" onclick="getStatusesX();" value="Get recent tweets">
<p id="tweetbox"></p>
</div>
接受输入并连接到 php 的脚本:
var intervalstop;
function getStatusesX() {
//trying to stop an empty input from executing but not working
if(input == ""){
alert("PLease enter a Twitter ID");
return false;
}
clearInterval(intervalstop);
var userID = document.getElementById("userid").value;
getStatuses(userID);
intervalstop = setInterval(function() {getStatuses(userID);}, 20000);
}
//Create a cross-browser XMLHttp Request object
function getXMLHttp() {
var xmlhttp;
if (window.ActiveXObject) {
XMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
} else if (window.XMLHttpRequest) {
XMLHttp = new XMLHttpRequest();
} else {
alert("Your browser does not support XMLHTTP!");
}
return XMLHttp;
}
//function that searches for the tweets via php
function getStatuses(userID){
XMLHttp1 = getXMLHttp();
//var userID = document.getElementById("userid").value;
//ajax call to a php file that will extract the tweets
XMLHttp1.open( 'GET', 'TwitterGlimpsePHP.php?userid='+userID, true);
// Process the data when the ajax object changes its state
XMLHttp1.onreadystatechange = function() {
if( XMLHttp1.readyState == 4 ) {
if( XMLHttp1.status ==200 ) { //no problem has been detected
document.getElementById("tweetbox").innerHTML=XMLHttp1.responseText;
}
}
}
XMLHttp1.send(null);
}