Hi there,
today I have written my first PHP script (and it even works, how cool!) and I have few questions.
Here's the script

<?php
$to = "myemail@myemail.com";
$subject = "Comments on page";
$message = $_POST["comments"];
mail($to,$subject,$message);
echo "Thank you for sending your feedback. That will help me improve the website.";
?>

and this is the element it applies to (I am testing it onto this page http://antobbo.webspace.virginmedia.com/webediting/documents.htm)

<form action="email.php" method="post">
<p><input type="button" value="Comment" onclick="showCommentBox()"><br></p>
<div class="hidden" id="comment">
<p>Comments and suggestions:<br><textarea name="comments" rows="3" cols="30"></textarea><br><br>
<input type="submit" value="Send"></p>
</div> <!-- end of comment_box-->
</form>

Now, the action page is email.php which then displays a message upon form submission (I still need to style this page a bit so it has just a simple message at the moment) and I kind of wondered how I can after say 5-10 seconds redirect people back to my website automatically rather than leaving them on the php page. Do I need to do that in PHP or can I do that in Java script? And where would I place this new script (or should be part on another script?!)...sorry I hope I am making some sense!
thanks

in email.php put this (before any html if you insert any later):

header("Location: $HTTP_REFERER");

Alternatively you can use this HTML for more control over the redirect:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Your Page Title</title>
<meta http-equiv="REFRESH" content="0;url=http://www.the-domain-you-want-to-redirect-to.com"></HEAD>
<BODY>
Optional page text here.
</BODY>
</HTML>

The 0 at the beginning of content is the timeout for redirection in seconds.

hello,

well , if u want to stay on the same page an ajax request could do wonders,
just paste the code without leaving any jquery. also just keep the jquery.js file whichi have attached in the root directory and observe the effect.

This is First page

<script src="jquery.js" type="text/javascript"></script>

        <script type="text/javascript">
$(document).ready(function(e) {
$(".results").hide();
   $(".mycomment").click(function () {
                    var currentcomm= $('textarea').attr('value');
                    alert(currentcomm)
                    $.post("email.php", {comments:currentcomm},
                    function(data) {
                $('.results').html(data).show();

            });

    $(".results").animate({

    width: "350px",
    height: "250px",
    opacity: 0.8,
    marginLeft: "0.6in",
    fontSize: "15px",
    borderWidth: "10px"                                                             

                }, 1500 );      

                });

         });

             </script>
<form name="form2" method="post">
<p><input type="button" value="Comment" onclick="showCommentBox()"><br></p>
<div class="hidden" id="comment">
<p>Comments and suggestions:<br><textarea  name="fname" id="fname"rows="3" cols="30"></textarea><br><br>
<input type="button"  class="mycomment" value="Send" ></p>
</div> 
<!-- end of comment_box-->
</form>
<div class = results style="position:absolute; top:200px;left:300px;background-color:#bca;border:1px solid green;text-align:center;" ></div>

PUT THIS or REPLACE YOUR EMAIL.PHP file by the below code

<?php
$to = "yogefriends@gmail.com";
$subject = "Comments on page";
$message = $_POST['comments'];
mail($to,$subject,$message);
echo "Thank you for sending your feedback. That will help me improve the website.";
echo '<input type="button" class="close" name="close" value="Close">';
?>
<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $('.close').click(function () {
        location.reload();

    });
</script>

IF you dont understand how to do , let me know

Sorry I don't mean to sound silly but in

header("Location: $HTTP_REFERER");

do I need to replace "location" with the URL I want people to go back to?

Also you said it goes before any HTML, so does it go also before the doctype? SOrry but I couldn't find anything about this...

yoge911 thanks for posting the code but I think for now I will stick to java script and a bit of PHP, not ready for Ajax and jquery yet :)

Ok thanks for that, but I can see some problems here.
If I have the php file like

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<?php
header('Location: http://www.antobbo.webspace.virginmedia.com/webediting/documents.htm');
$to = "antobbo@gmail.com";
$subject = "Comments on page";
$message = $_POST["comments"];
mail($to,$subject,$message);
echo "<p>Thank you for sending your feedback. That will help me improve the website.</p>";
?>
<html>
<title>Thank you</title>

<body>
</body>
</html>

with the header before any output hte html code won't validate and the script also (when it loads the php page) returns an error that reads:
"Warning: Cannot modify header information - headers already sent by (output started at /tier-11/pwpstore3/1/antobbo/htdocs/webediting/email.php:4) in /tier-11/pwpstore3/1/antobbo/htdocs/webediting/email.php on line 5
Thank you for sending your feedback. That will help me improve the website."

I then cannot have this I suppose because the header needs to appear before any output:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<title>Thank you</title>
<?php
header('Location: http://www.antobbo.webspace.virginmedia.com/webediting/documents.htm');
$to = "antobbo@gmail.com";
$subject = "Comments on page";
$message = $_POST["comments"];
mail($to,$subject,$message);
echo "<p>Thank you for sending your feedback. That will help me improve the website.</p>";
?>


<body>
</body>
</html>

This validates...so I wonder, can I have something like:

<?php
header('Location: http://www.antobbo.webspace.virginmedia.com/webediting/documents.htm');
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<title>Thank you</title>
$to = "antobbo@gmail.com";
$subject = "Comments on page";
$message = $_POST["comments"];
mail($to,$subject,$message);
echo "<p>Thank you for sending your feedback. That will help me improve the website.</p>";
?>


<body>
</body>
</html>

or is it considered heresy?
The thing is that I need to make sure that the html code validates

Sounds good, just why not close and reopen the php tags?

<?php
header('Location: http://www.antobbo.webspace.virginmedia.com/webediting/documents.htm');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<title>Thank you</title>
<?php
$to = "antobbo@gmail.com";
$subject = "Comments on page";
$message = $_POST["comments"];
mail($to,$subject,$message);
echo "<p>Thank you for sending your feedback. That will help me improve the website.</p>";
?>


<body>
</body>
</html>

Ah ok, sorry didn't know that was possible I thought everything had to be within the same angle brackets.
I tried what you suggested burgercho but something really funny happen. If I keep my script as it was

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<title>Thank you</title>
<body>
<?php
$to = "antobbo@gmail.com";
$subject = "Comments on page";
$message = $_POST["comments"];
mail($to,$subject,$message);
echo "<p>Thank you for sending your feedback. That will help me improve the website.</p>";
?>
</body>
</html>

the comment gets sent to my email address, if I change to the above, I receive no comment at all, I have just tested...not sure why that is.
One more thing I forgot to mention was that I would like people to see the thank you page, and then get redirected back to the source page, but with the script you suggested when I submit the form I can't see the thank you page at all
thanks

Then you should probably just use an HTML redirect with a timeout:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<meta http-equiv="REFRESH" content="5;url=http://www.antobbo.webspace.virginmedia.com/webediting/documents.htm">
</head>
<title>Thank you</title>
<body>
<?php
$to = "antobbo@gmail.com";
$subject = "Comments on page";
$message = $_POST["comments"];
mail($to,$subject,$message);
echo "<p>Thank you for sending your feedback. That will help me improve the website. </p>";
?>
</body>
</html>

Just change the number before content (right now is 5) towhatever you wan the timeout to be in seconds.

thank you burgercho that worked well and it validates too (which I think is a bit odd, I would have thought it won't validate it because we're taking the control away from the user).
I also moved the <title> within the <head> as per DOCTYPE.
So the same thing is not achievable using solely PHP? I mean isn't there any code that allows you to redirect a user back to a page (providing that it shows you the submission page of course)?

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.