如何在没有 window.open 的情况下从 Javascript 启动 PHP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10734396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to launch PHP file from Javascript without window.open
提问by Hataru
I'm using Javascript and I need to call a PHP file to execute some stuff, but I'd like not to use window.open("filename.php");
because it usually opens a new tab or new window.
我正在使用 Javascript,我需要调用一个 PHP 文件来执行一些东西,但我不想使用window.open("filename.php");
它,因为它通常会打开一个新选项卡或新窗口。
Sometime a PHP file need a few seconds to finish its job and I don't like to see an open tab or open window while it's working.
有时一个 PHP 文件需要几秒钟才能完成它的工作,我不喜欢在它工作时看到打开的选项卡或打开的窗口。
Thanks
谢谢
回答by Cyril F
AJAX is the solution, and with jQuery it's so simple.
AJAX 就是解决方案,而 jQuery 则非常简单。
Just add this line on your head section (to include jquery):
只需在 head 部分添加这一行(包括 jquery):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.ajax({
type: "GET",
url: "yourPage.php"
}).done(function( data) {
alert( "Request is done" );
});
</script>
回答by yAnTar
回答by Vishal
Use Ajax! Here's a solution using JavaScript's jQuery library:
使用阿贾克斯!这是使用 JavaScript 的 jQuery 库的解决方案:
$.post('your_file.php', {parameter : some_value}, function(response){
// now you can use `response` came from your_file.php after execution
});
$.post()
, $.get()
are shorthand methods for $.ajax()
of jQuery.
$.post()
,$.get()
是$.ajax()
jQuery 的简写方法。
or simply use .load(),
或者干脆使用.load(),
('#test_div').load('your_file.php'); //load data from server into `#test_div`
will do job, if you want to just execute something on server-side.
如果您只想在服务器端执行某些操作,则可以完成工作。