2

I am absolutely new to Wordpress, PHP and Angularjs so please bear with me.

I have two custom pages, let's call them page-A and page-B.

I want to be able to click on a link from page-A and open page-B on a new tab passing to it a parameter.

The parameter is a very long string of text, so I think using POST would be more appropiate.

Note: I am using Angularjs and not JQuery.

These are the things I've been trying with no success:

This opens a new tab with page-B but doesn let me pass the string parameter that I need:

url = "<?= get_page_link(115) ?>";
window.open(url,'_blank');

This allows me to send the parameter but doesn't open a new tab:

url = "<?= get_page_link(115) ?>";
return $http({
        url: "<?= get_page_link(115) ?>",
        method: 'POST',
        params: {
            event_id:event_id,
            texto:event.text,
        },
        data: this.text
});

So my question is: how could I open a new tab to show my custom page page-B passing it with a parameter?

Kevin
  • 41,694
  • 12
  • 53
  • 70
Xar
  • 7,572
  • 19
  • 56
  • 80

2 Answers2

3

I don't know what get_page_link(115) would give you but here's an example of passing parameters to php using new tabs.

javascript:

var test1 = "Hello";

var test2 = "World";

url = "ExamplePage.php?Val1=" + test1 + "&Val2=" + test2;

window.open(url,'_blank');

ExamplePage.php:

$Var1 = $_GET['Val1'];
$Var2= $_GET['Val2'];

echo $Var1;
echo $Var2;

Output would be: "Hello World"

  • Thank you @jbyrne2007. Using GET implies that I can only send strings of a certain (rather small compared to what I need) size. For this reason I prefer using POST in this case. – Xar Jul 24 '14 at 13:29
  • Take a look at http://stackoverflow.com/questions/8919019/how-to-post-form-data-to-a-new-window-using-jquery-ajax –  Jul 24 '14 at 13:52
2

Anchor links ( tags) only trigger GET requests. In other hand if parameter is long long string, GET cannot be used because URL max length constraint. If you want to redirect to another page using POST, you need to perform the follow steps:

  1. When the user clicks on Anchor Link in page-A, build via javascript a form over the air, the form action needs to be setup to page-B URL, and target _blank like anchor links.
  2. Adds to the new created form, all input hidden fields do you need.
  3. Call submit event for this Over-the-air form.

     <script type="text/javascript">
     function createFormAndSubmit(targetUrl, parameter) {
         var form = document.createElement("form");
         document.body.appendChild(form);
         form.method = "POST";
         form.action = targetUrl;
         form.target = "_blank";
         var element1 = document.createElement("INPUT");         
         element1.name="texto"
         element1.value = parameter;
         element1.type = 'hidden';
         form.submit();
     }
    
     url = "<?= get_page_link(115) ?>";
     parameterValue = "LONG STRING HERE";
    
     createFormAndSubmit(url, parameterValue);
     </script> 
    
Mr Rivero
  • 1,248
  • 7
  • 17
  • Thank you Kellerman. I have tried your code and it has certainly helped me get closer to what I want to do. But I am still having a problem. On the `page-B` php code I print the POST parameters that the page receives like this `print_r($_POST);` and I just get an empty array. The text parameter doesn't seem to be received. Any idea what I might be missing? – Xar Jul 24 '14 at 13:27
  • If you are using Wordpress, the Wordpress core may can remove your parameter based on the name, instead of using 'texto' as name of parameters, use something like **myprefix_parametername**. If you use Chrome as Web browser, you can inspect the POST request was you sent, using Developer Tools, Network Tab, check if all parameters are send correctly. – Mr Rivero Jul 24 '14 at 15:02