1

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.

4 Answers 4

4

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Do not use escape()/unescape(). There's proper functions for different purposes. In this case, encodeURIComponent is appropriate.
1

It's simpler than you'd think.

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

Comments

1

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

Comments

0

use

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

1 Comment

I tried all the above three but the resulting page is the same page from which the function was called with the variable attached to it. from localhost/thirdPage.php?p=tableName to localhost/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.