2012-08-14 3 views
0

AJAX 및 PHP를 사용하여 버튼을 눌렀는지 여부를 감지하려고합니다 (물론 실제 목표가 아닌 테스트의 일부 임). 내가 텍스트 상자에 뭔가를 입력 할 때마다 나는 감지 할 수 있지만 버튼이 눌러 졌는지 아닌지를 감지 할 수는 없다. 당신의 버튼이 form에없는 경우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!'; 
} 
?> 
+1

'삽입'은 어디에서 호출 되었습니까? – Musa

+0

대답이라면 나는 이것을 틱 할 것이다. –

답변

1

, 당신은 단순히 <button> 또는 <input type="button">를 사용할 수 있습니다.

<button onclick="insert();">Search!</button>을 단추 누르기 이벤트 수신자로 사용할 수 있습니다.

+0

Infact 나는 어떤 이유로 인해 그것을 삭제하고 그것을 다시 잊어 버렸습니다, 오늘은 누군가가 내 어리석은 실수를 지적 두 번째 시간입니다. 어쨌든 잘 자리 잡았어. –

관련 문제