2011-05-23 66 views
1

我有一个php页面first.php,我想通过从javascript函数传递一些参数打开页面。你能帮我一下,谢谢。从javascript函数打开php页面

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'); 
} 

但我无法打开该页面,请帮忙。提前致谢。

回答

0

使用

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

我尝试了所有上述三个,但随后出现的页面是从中调用函数与它相连的变量在同一页。 从 HTTP://localhost/thirdPage.php P = TABLENAME 到 HTTP://localhost/thirdPage.php checkbux = R&checkbux = V&checkbux = Y – chella 2011-05-23 23:45:00

1

它比你想象的简单。

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

由于以前的答案状态,您可以很容易地使用window.location打开一个PHP页面;然而,你应该永远记住的URL使用它们时,使用encodeURIComponent() JavaScript函数来逃避变量:

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

不要使用转义()/ UNESCAPE( )。有适合不同目的的功能。在这种情况下,encodeURIComponent是适当的。 – Krinkle 2012-01-25 01:49:15

1

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

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