-1

Possible Duplicate:
Headers already sent by PHP

if($sendmail) {
  echo "<div class=\"success\" class=\"regdet\">Congratulations $name! You have successfully registered for <span>AADAB 2013</span>! An email has been sent to your email with the details of your form.<br><br>If the page does not automatically redirect you in 5 seconds, click <input type=\"button\" value=\"Here\" onclick=\"window.close()\"> to continue.</div>";
  header (refresh: 5, url="form.php");
} else {
  echo "<span style=\"color: red; background: rgba(219,219,219,0.7);\">There was an error in submitting your form. Click <a href=\"form.php\">here</a> to try again!</span>";
}

I am trying to redirect the user to the form (which is the current form which has both, the form and the PHP commands) after 5 seconds ... since it is not happening, I am trying to include a button which will close the window after it is pressed on the form confirmation page.

That too is not occurring.

the message is being displayed and the form is delivering emails to my email account perfectly, but this small part is bugging me a lot !

Community
  • 1
  • 1

4 Answers4

1

You should pass the paramaters to header() as a string (you are in fact sending a raw header string).

So like this:

header('refresh:5;url=form.php');

And as to the window.close(). If you haven't opened the window with Javascript and try to close it using a handler to the window you may in fact experience problems. Javascript won't let you just close any window the user sees because of security reasons. In theory you should see a browser alert telling you that JS wants to close this window (but I didn't manage to get it working on IE 9 or FF).

Michal Trojanowski
  • 10,641
  • 2
  • 22
  • 41
0

"This method is only allowed to be called for windows that were opened by a script using the window.open method. If the window was not opened by a script, the following error appears in the JavaScript Console: Scripts may not close windows that were not opened by script."

When you opened the Window this should of been assigned to a variable.

openedWindow = window.open('moreinfo.htm');

Then when you want to close it...

openedWindow.close();

If your wanting to close the window with JS you might aswell then redirect with JS aswell...

location.href = 'form.php';
Alex Gill
  • 2,395
  • 1
  • 16
  • 18
0

You can only output headers if you have not already outputted anything to the browser. In your case you echo something, then try to send a header().

Also you should quote your argument:

// no output so far....
header ('refresh: 5; url=form.php');

If you view your error log, you should find:

PHP Warning: Cannot modify header information - headers already sent by ...

MrCode
  • 63,975
  • 10
  • 90
  • 112
  • yes ... but i tried it the other way around too ... i put the header() command at the top (as soon as $sendmail returns as true) ... nothing happens even then – anubhav gupta Jan 18 '13 at 09:55
  • Have you checked your error log? You could still be outputting something before the send mail call. Even a space character before the opening ` – MrCode Jan 18 '13 at 09:56
  • yes .. firebug shows nothing .. i tried phpcodechecker.com as well ... in case it helps im trying to do the above at www.cookingquery.com/aadab/form.php – anubhav gupta Jan 18 '13 at 09:59
  • No I mean your PHP error log, not Firebug. It will have the header warning and also a syntax error because you aren't quoting the argument. – MrCode Jan 18 '13 at 10:00
-1

window.close() doesn't work on it's own in many modern browsers.

There are ways around it: Issue with window.close and chrome

Or is it possible for you to use a modal popup rather than a new window?

Community
  • 1
  • 1
Grim...
  • 16,518
  • 7
  • 45
  • 61