使用 AJAX 和 PHP 检测按钮按下


Detecting button press using AJAX and 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>侦听器到按钮按下事件。