click on the button to run php code and echo result

社会主义新天地 提交于 2020-01-24 00:47:12

问题


I want to implement this script: http://www.daniloaz.com/en/560/programming/backup-de-bases-de-datos-mysql-con-php/

on my website to backup all the database at the end of the day.

May I know is it possible for me to put a button to click and run the script and echo the status after it finish without refreshing the page or bring me to a new page.

as at the moment i everyday need to go to the link: www.example.com/backup.php to do daily backup, I want this to ease my job :)

any idea how to do this?


回答1:


what type of button? if html yes it's easy

have a button on what ever page you want along with a div like so:

<input type="button" name="perform_backup"  onclick="do_backup()"><div id="results"> No backup has been performed yet</>

then have a javascript which calls your backup php script like this

<script>  
 function do_backup(){
   var xhReq = new XMLHttpRequest();
   var request = "http://www.example.com/backup.php " // this will prepare a request to your server
   xhReq.open("GET", request, false);  // send a request
   xhReq.send(null);
   document.getElementsByID("results").innerHTML=xhReq.responseText  /// this will display results

   }
</script>

then I would modify the script on your server to give more then a boolean variable but that's up to you. But whatever you choose it will be seen inside <div id="results"> , without loading a new page... This method is called AJAX for future questions



来源:https://stackoverflow.com/questions/16377985/click-on-the-button-to-run-php-code-and-echo-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!