Open a php page from javascript function

后端 未结 4 2139
生来不讨喜
生来不讨喜 2021-01-25 17:13

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() {
          


        
相关标签:
4条回答
  • 2021-01-25 17:28

    It's simpler than you'd think.

    window.location = 'http://localhost/first.php?q=' + checkB + '&p=' + tableName;
    
    0 讨论(0)
  • 2021-01-25 17:30

    use

    document.location = 'http://localhost/first.php?q='+checkB+'&p='+tableName;
    
    0 讨论(0)
  • 2021-01-25 17:34

    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>");
    }
    
    0 讨论(0)
  • 2021-01-25 17:52

    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);
    
    0 讨论(0)
提交回复
热议问题