使用AJAX和PHP检测按钮按下
问题描述:
我试图检测是否使用AJAX和PHP按钮(当然这是测试的一部分,而不是我的实际目标)。每当我在文本框中输入内容但无法检测按钮是否被按下时,我都可以检测到它。使用AJAX和PHP检测按钮按下
<html>
<head>
<script type="text/javascript">
function insert(){
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest();
else
xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById('adiv').innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open('GET','AJAX.inc.php?search='+document.getElementById('search_text').value+'search_button=Search!', true);
xmlhttp.send();
}
</script>
</head>
<body>
<input type="text" id="search_text" name="search" onkeyup="load();">
<input type="submit" name="search_button" value="Search!">
<div id="adiv"></div>
</body>
</html>
And here's my AJAX file:
<?php
if(isset($_GET['search_button'])){
echo 'You pressed a button!';
}
?>
答
如果您的按钮不在form
,你可以简单地使用<button>
或<input type="button">
。
您可以使用<button onclick="insert();">Search!</button>
来监听按钮按下事件。
+0
Infact我由于某种原因将其删除,并忘记将其添加回来,这是今天第二次有人指出我愚蠢的错误。无论如何,当然,你值得剔。 – 2012-08-14 05:05:45
调用insert函数在哪里? – Musa 2012-08-14 04:52:36
如果是答案,我会选中它。 – 2012-08-14 05:06:15