카테고리 없음
[PHP] 버튼에서 PHP 함수를 호출하는 방법은 무엇입니까?
행복을전해요
2020. 12. 11. 09:23
자바 스크립트의 XMLHTTPRequest (또는 jQuery의 get ) 를 사용해야 합니다. 두 경우 모두 자바 스크립트를 사용하면 다른 URL에서 콘텐츠를 동적으로로드 할 수 있습니다.
-------------------ajax를 사용하여 응답을 받고 버튼 아래에 표시하려면 먼저 버튼 아래에 메시지 공간을 만드세요.
<form onclick='run()'>
<button type="submit">Pull changes</button>
<div id='msg'><div/>
</form>
이제 헤더 태그에 다음을 추가하십시오.
<script type="text/javascript">
function run()
{
loadXMLDoc("pull.php",function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('msg') = xmlhttp.responseText;
}
});
}
function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("POST",url,true);
xmlhttp.send();
}
</script>
-------------------PHP는 서버 측 언어이며 버튼 클릭은 클라이언트 측 작업이므로 JavaScript와 같은 클라이언트 측 솔루션을 사용하여 간격을 메워야합니다.
-------------------action 속성을 비워두고 버튼 바로 뒤에 해당 코드를 넣으면 다음과 같이됩니다.
<form metod="post" action="">
<button name="button" type="submit">Pull changes</button>
</form>
<?php
if(isset($_POST['button'])) {
$command = "git pull";
$output = shell_exec($command);
echo "<pre>$output</pre>";
}
?>
출처
https://stackoverflow.com/questions/7415015