Opening new page and then running header

Asked

Viewed 3,353 times

-1

I currently have a button (print) that performs a Submit in the form (since I need some fields that are in this form). This in turn executes the code that is in the action, where I have the following code:

echo "<script>window.open('".$url."', '_blank');</script>";

Well, if I run the script this way, a new tab is opened in the browser, but the original page occurs an error (indexed variable that does not receive value). To get around this error, I thought to run a header.

Thus remaining:

header('location:../paginaOriginal.php');
echo "<script>window.open('".$url."', '_blank');</script>";
exit;

But when I set the header, the new tab is not opened, simply reload the page.

Button script:

    $("#botao1").click(function(){
        $("#form").submit();
});

The ideal would be the original page, remain static, because it is a print button. However I need some fields that are in the form of this page and put this data in a layout already ready.

  • You can give no way out (for example, no echo) before the headers, they should be the first thing to be sent by the server. See http://answall.com/questions/4251/erro-do-php-cannot-modify-header-information

  • I’m not sure what you’re getting at. It seems like you don’t want the form to be submitted, but at the same time you’re forcing it to be. What error occurs on the original page?

  • Which error appears? ever tried to put an onready to wait for page loading ?

  • @bfavaretto, if it were possible to open this new tab with the information I am sending in the $url, it would be ideal, but I could not do it. Performing echo after header, the tab is not opened.

  • The error refers to a variable that has been indexed but receives no value.

  • Edit the question to put exactly what the error message was. I understand that you want to open another tab with this URL, but what do you want to happen with the original page? What is the form for? You need to give more context so we can indicate the best way.

  • @bfavaretto, see if you’re better. I edited the question as you had requested. Thank you.

  • I don’t understand why you submit a form you don’t want submitted! When you submit it, it reloads the page. That’s at least part of the problem.

  • Try to put window.open within $(Document). ready(), so it will run after the page loads

  • The header (php) runs from the server side. header('location:../paginaOriginal.php');&#xA;echo "<script>window.open('".$url."', '_blank');</script>"; will never perform the window.open, because the browser does not render pages with server-side forwarding.

Show 5 more comments

1 answer

1

I believe you don’t need PHP for this function. send your post to a new tab.

<form action="pagina.php" type="post" target="_blank">
<input name="test" value="1" />
<input type="submit" value="Submit">     
</form>

php page.

<?php
//seu código vai aqui
//--------------
//redirect se você realmente precisa disso
header('location:'.$url);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.