从 javascript 函数打开 php 页面

发布于 2024-11-08 22:20:30 字数 366 浏览 0 评论 0原文

我有一个 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');
}

但是打不开页面,请帮忙。提前致谢。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柳若烟 2024-11-15 22:20:30

正如前面的答案所述,您可以轻松地使用 window.location 打开 PHP 页面;但是,在 URL 中使用变量时,您应该始终记住使用 encodeURIComponent() JavaScript 函数对变量进行转义:

window.location = "http://localhost/first.php?q=" + encodeURIComponent(checkB) + "&p=" + encodeURIComponent(tableName);

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 = "http://localhost/first.php?q=" + encodeURIComponent(checkB) + "&p=" + encodeURIComponent(tableName);
执着的年纪 2024-11-15 22:20:30

这比你想象的要简单。

window.location = 'http://localhost/first.php?q=' + checkB + '&p=' + tableName;

It's simpler than you'd think.

window.location = 'http://localhost/first.php?q=' + checkB + '&p=' + tableName;
狼亦尘 2024-11-15 22:20:30

我的理解是,您需要打开另一个带有一些动态参数的选项卡或弹出窗口。
我对此有 2 个解决方案:

1- 将一些额外的 JS 附加到锚点,用户将使用 onMouseOver() 事件进行单击,并使用计算出的 URL 提供 href。目标必须设置为“_blank”。

示例:

<a href="whateverPage.php" target="_blank" onMouseOver="this.href='myPage.php?myParam=' + myParamValue;">Goto new page</a>

请注意,在此示例中“myParamValue”需要是全局的。

2- 您想在 ajax 请求后打开一个新选项卡或弹出窗口吗?就我而言,我想在服务器上生成一个新的报告 PHP 页面并希望立即打开它。以前的解决方案没有帮助。

这是我愚弄弹出窗口拦截器的解决方案:

//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>");
}

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".

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.

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.

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>");
}
冰雪梦之恋 2024-11-15 22:20:30

使用

document.location = 'http://localhost/first.php?q='+checkB+'&p='+tableName;

use

document.location = 'http://localhost/first.php?q='+checkB+'&p='+tableName;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文