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>"; 
}
?>

Recommended Answers

All 3 Replies

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.

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.

many thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.