Can php close a browser window or tab?

I made a contact form. The form calls a php file. This successfully saves surname, name, email, message and a file to my webpage host.

At the bottom of upload1file2.php I put some html to say “Success! File uploaded, details saved”

I also want to have 2 html buttons. 1 links back to the form page. That works OK.

I would like another button to close the tab in the browser if the user is finished.

I have this html, but it does not work. Apparently, the problem is, if the script did not open the page,
window.close() wil not shut it. I will try to tweak it to shut the page.

<ul style="display:inline-table; width:950px;">
<li> <a href="http://www.mywebpage.com/form1.html">Go back<p>返回</p></a> </li>
<li> <a href="javascript:window.open('','_self').close();">close</a> </li>
<li> <a href="#" id="close-window">Close this window tab</a> </li>
</ul>
</div>
<script type="text/javascript">
  document.getElementById("close-window").addEventListener("click", function(){
  		 window.open('','_parent',''); # this is supposed to fool the browser that this script opened the webpage
       window.close();
   });
</script>

My questions therefore is: As upload1file2.php opens this success-page: Can I use php to make it close?

Why don’t you just use the X on the browser tab or hit ctrl+w on your keyboard? Most browsers have this feature and i don’t see why you would emulate that.

And on your main question:

Can php close a browser window or tab

if you are willing to involve several steps until you reach the fronent and you want to create a large chain of custom software - yes.

There isn’t a cross-browser way do switch a tab that’s 100% reliable, especially in newer browsers. It’s actively blocked in most newer browsers. As a user, I’m glad it’s difficult in most cases, impossible in others to do this. And why would you open another window to give a status message? You can better use a Modal Box which will work in any modern browser on any manner you like…

Thanks for the tip about modals! Great! It will take me a while to implement it, I’m very slow!

I only wanted a ‘close this tab’ button because I have seen them on other webpages. I thought it would be easy. Not important!

This is all for my little homework webpage.

When the students click ‘Send answers’ the form calls a thankyou.php, which which sends the answers to me as an email.

$to1 = "myemail@mail.com";
$subject = $studentnr . "18BEwW1";
$headers = "From: peter@mywebpage.com\r\n";
$headers .= 'Content-Type: text/plain; charset=utf-8';
mail($to1, $subject, $body, $headers);
header("Location: email_success.php");

This bit: header(“Location: email_success.php”); opens the other tab. That is how I learnt to do it in a tutorial.

Email sent!

发送成功

Yippee, your email has been sent to Peter!

太好了你的邮件发送成功

Now I will see if I can get that in a modal! Thanks for any tips!

Modals are just some html and css that will be hidden by default with css property display set to none (display: none;) that you can popup just by changing the value to block (display: block;).
Because they are just HTML you can simply integrate them anywhere in your body block of your html page and trigger them to visible/invisible with a little javascript.

Showing the modal after a form submit - which is what you want if i understand you right - is a little different. You could redirect the user to any other page with your header function but this time you would like to show the modal immediately after the page have been loaded. you can do this easily when you remove the display: none; css rule. with PHP you can decide when to add the modals HTML or not, for example depending on a $_GET variabele e.g. http://mydomain.org/mypage.php?showmodal=1

1 Like

Great, thanks. I’ll try to get it working. Always takes me a while!

Sponsor our Newsletter | Privacy Policy | Terms of Service