简体   繁体   English

如何在没有window.open的情况下从Javascript启动PHP文件

[英]How to launch PHP file from Javascript without window.open

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"); 我正在使用Javascript,并且需要调用PHP文件来执行一些操作,但是我不想使用window.open("filename.php"); because it usually opens a new tab or new window. 因为它通常会打开一个新标签或新窗口。

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 谢谢

AJAX is the solution, and with jQuery it's so simple. AJAX是解决方案,而使用jQuery则是如此简单。

Just add this line on your head section (to include jquery): 只需在您的头部添加此行(以包含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>

You can use ajax or dynamically create iframe. 您可以使用Ajax或动态创建iframe。

If you use extjs library - here is example ajax request 如果您使用extjs库- 是ajax请求示例

Use Ajax! 使用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()是jQuery $.ajax()的简写方法。

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. 如果您只想在服务器端执行某些操作,它将可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM