javascript 从javascript函数打开一个php页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6103770/
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
Open a php page from javascript function
提问by chella
I have a php page, first.php and I want to open the page with passing some arguments from a javascript function. Could you please help me, thanks.
我有一个 php 页面,first.php,我想通过从 javascript 函数传递一些参数来打开该页面。能不能帮帮我,谢谢。
function() {
var tableName = "<?= $p ?>"; //obtaining the value from another php file
var checkB = checkbox_form.checkbux[counter].value;
window.open('"http://localhost/first.php?q="+checkB+"&p="+tableName', '_self');
}
But I am not able to open the page, please help. Thanks in advance.
但是我打不开页面,请帮忙。提前致谢。
回答by Frxstrem
As the previous answers state, you could easily use window.location
to open a PHP page; however, you should always remember to escape your variables when using them in a URL, using the encodeURIComponent()
JavaScript function:
正如前面的答案所述,您可以轻松地使用window.location
来打开一个 PHP 页面;但是,在 URL 中使用变量时,您应该始终记住使用encodeURIComponent()
JavaScript 函数对变量进行转义:
window.location = "http://localhost/first.php?q=" + encodeURIComponent(checkB) + "&p=" + encodeURIComponent(tableName);
回答by Damien Goor
My understanding is that you need to open another tab or popup with some dynamic parameters. I have 2 solutions for this:
我的理解是您需要打开另一个带有一些动态参数的选项卡或弹出窗口。我有两个解决方案:
1- Attach some extra JS to the anchor user will click using the onMouseOver() event and feed the href with your computed URL. The target must be set to "_blank".
1- 将一些额外的 JS 附加到锚点用户将使用 onMouseOver() 事件单击并使用您计算出的 URL 提供 href。目标必须设置为“_blank”。
Example:
例子:
<a href="whateverPage.php" target="_blank" onMouseOver="this.href='myPage.php?myParam=' + myParamValue;">Goto new page</a>
Note that in this example 'myParamValue' needs to be global.
请注意,在此示例中,“myParamValue”需要是全局的。
2- You want to open a new tab or pop up after an ajax request? In my case I want generate a new report PHP page on the server and want to open it immediately. Previous solution does not help.
2- 您想在 ajax 请求后打开一个新选项卡或弹出吗?在我的情况下,我想在服务器上生成一个新的报告 PHP 页面并想立即打开它。以前的解决方案没有帮助。
Here is my solution to fool the pop-up blockers:
这是我愚弄弹出窗口阻止程序的解决方案:
//this generates the new report page
report = new ajaxReq("gentabrep.php", ajaxCallBackFunction);
//open the pop-up on user action/event which is normally allowed
w = window.open("", "");
//run ajax request, note I also pass the "w" pop-up reference to the request
report.request("connId=" + connId + "&file=" + file, "POST", [w, file]);
function ajaxCallBackFunction(returnedStr, status, params){
//I feed the pop-up with the necessary javascript to redirect the page immediately
params[0].document.writeln("<scr"+"ipt type='text/javaScript'>window.location='reports/" + params[1] + ".php';</scr"+"ipt>");
}
回答by Kyle Sletten
It's simpler than you'd think.
它比你想象的要简单。
window.location = 'http://localhost/first.php?q=' + checkB + '&p=' + tableName;
回答by Senad Me?kin
use
利用
document.location = 'http://localhost/first.php?q='+checkB+'&p='+tableName;