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() {
It's simpler than you'd think.
window.location = 'http://localhost/first.php?q=' + checkB + '&p=' + tableName;
use
document.location = 'http://localhost/first.php?q='+checkB+'&p='+tableName;
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>");
}
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);