0

I am try to put the finishing touches to a mail function, and would like to show an alert to tell the user the form has been submitted correctly. I would then like to redirect them to another page in the site.

The alert works OK on its own, but when I add a header to redirect, the redirect is OK but the alert does not work.....my code is below please help.

<?php
$ok = @mail($email_to, $email_subject, $email_message, $headers); 

if($ok) { 
echo "<script language='javascript'>";
echo "alert('Thank You!! Your booking has been made successfuly. To CANCEL please call xxx)";
echo "</script>"; 
echo header('Location:home.htm');
}else {
echo "<script language='javascript'>";
echo "alert('SORRY! A technical error has occurred, please Call xx to make your booking')";
echo "</script>"; 
}
?>

Edited by mike_2000_17: Fixed formatting

3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by Thorby68
0

Because you generated an output to browser, by making <script> tags and so on. Check the reference for header() function. There is said that NO OUTPUT should be BEFORE sending headers, not even a space or line feed.

Instead of headers, you can use "document.location.href" javascript property. That is redirect too.

Edited by amd_k8: n/a

0

I am try to put the finishing touches to a mail function, and would like to show an alert to tell the user the form has been submitted correctly. I would then like to redirect them to another page in the site.

The alert works OK on its own, but when I add a header to redirect, the redirect is OK but the alert does not work.....my code is below please help.

<?php
$ok = @mail($email_to, $email_subject, $email_message, $headers); 

if($ok) { 
echo "<script language='javascript'>";
echo "alert('Thank You!! Your booking has been made successfuly. To CANCEL please call xxx)";
echo "</script>"; 
echo header('Location:home.htm');
}else {
echo "<script language='javascript'>";
echo "alert('SORRY! A technical error has occurred, please Call xx to make your booking')";
echo "</script>"; 
}
?>

end quote.

//change to this
if($ok) { 
echo "<script language='javascript'>";
echo "alert('Thank You!! Your booking has been made successfuly. To CANCEL please call xxx)";
echo"window.location='home.htm'"
echo "</script>"; 
//echo header('Location:home.htm');

you needed to make the call in javascript. you can not use headers once output has begun.

Edited by mike_2000_17: Fixed formatting

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.