0

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

3
Contributors
9
Replies
14
Views
6 Years
Discussion Span
Last Post by Violet_82
0

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.

0

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

Edited by Nick Evan: Fixed formatting

Attachments
/*
 * jQuery JavaScript Library v1.3.2
 * http://jquery.com/
 *
 * Copyright (c) 2009 John Resig
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 *
 * Date: 2009-02-19 17:34:21 -0500 (Thu, 19 Feb 2009)
 * Revision: 6246
 */
(function(){var l=this,g,y=l.jQuery,p=l.$,o=l.jQuery=l.$=function(E,F){return new o.fn.init(E,F)},D=/^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,f=/^.[^:#\[\.,]*$/;o.fn=o.prototype={init:function(E,H){E=E||document;if(E.nodeType){this[0]=E;this.length=1;this.context=E;return this}if(typeof E==="string"){var G=D.exec(E);if(G&&(G[1]||!H)){if(G[1]){E=o.clean([G[1]],H)}else{var I=document.getElementById(G[3]);if(I&&I.id!=G[3]){return o().find(E)}var F=o(I||[]);F.context=document;F.selector=E;return F}}else{return o(H).find(E)}}else{if(o.isFunction(E)){return o(document).ready(E)}}if(E.selector&&E.context){this.selector=E.selector;this.context=E.context}return this.setArray(o.isArray(E)?E:o.makeArray(E))},selector:"",jquery:"1.3.2",size:function(){return this.length},get:function(E){return E===g?Array.prototype.slice.call(this):this[E]},pushStack:function(F,H,E){var G=o(F);G.prevObject=this;G.context=this.context;if(H==="find"){G.selector=this.selector+(this.selector?" ":"")+E}else{if(H){G.selector=this.selector+"."+H+"("+E+")"}}return G},setArray:function(E){this.length=0;Array.prototype.push.apply(this,E);return this},each:function(F,E){return o.each(this,F,E)},index:function(E){return o.inArray(E&&E.jquery?E[0]:E,this)},attr:function(F,H,G){var E=F;if(typeof F==="string"){if(H===g){return this[0]&&o[G||"attr"](this[0],F)}else{E={};E[F]=H}}return this.each(function(I){for(F in E){o.attr(G?this.style:this,F,o.prop(this,E[F],G,I,F))}})},css:function(E,F){if((E=="width"||E=="height")&&parseFloat(F)<0){F=g}return this.attr(E,F,"curCSS")},text:function(F){if(typeof F!=="object"&&F!=null){return this.empty().append((this[0]&&this[0].ownerDocument||document).createTextNode(F))}var E="";o.each(F||this,function(){o.each(this.childNodes,function(){if(this.nodeType!=8){E+=this.nodeType!=1?this.nodeValue:o.fn.text([this])}})});return E},wrapAll:function(E){if(this[0]){var F=o(E,this[0].ownerDocument).clone();if(this[0].parentNode){F.insertBefore(this[0])}F.map(function(){var G=this;while(G.firstChild){G=G.firstChild}return G}).append(this)}return this},wrapInner:function(E){return this.each(function(){o(this).contents().wrapAll(E)})},wrap:function(E){return this.each(function(){o(this).wrapAll(E)})},append:function(){return this.domManip(arguments,true,function(E){if(this.nodeType==1){this.appendChild(E)}})},prepend:function(){return this.domManip(arguments,true,function(E){if(this.nodeType==1){this.insertBefore(E,this.firstChild)}})},before:function(){return this.domManip(arguments,false,function(E){this.parentNode.insertBefore(E,this)})},after:function(){return this.domManip(arguments,false,function(E){this.parentNode.insertBefore(E,this.nextSibling)})},end:function(){return this.prevObject||o([])},push:[].push,sort:[].sort,splice:[].splice,find:function(E){if(this.length===1){var F=this.pushStack([],"find",E);F.length=0;o.find(E,this[0],F);return F}else{return this.pushStack(o.unique(o.map(this,function(G){return o.find(E,G)})),"find",E)}},clone:function(G){var E=this.map(function(){if(!o.support.noCloneEvent&&!o.isXMLDoc(this)){var I=this.outerHTML;if(!I){var J=this.ownerDocument.createElement("div");J.appendChild(this.cloneNode(true));I=J.innerHTML}return o.clean([I.replace(/ jQuery\d+="(?:\d+|null)"/g,"").replace(/^\s*/,"")])[0]}else{return this.cloneNode(true)}});if(G===true){var H=this.find("*").andSelf(),F=0;E.find("*").andSelf().each(function(){if(this.nodeName!==H[F].nodeName){return}var I=o.data(H[F],"events");for(var K in I){for(var J in I[K]){o.event.add(this,K,I[K][J],I[K][J].data)}}F++})}return E},filter:function(E){return this.pushStack(o.isFunction(E)&&o.grep(this,function(G,F){return E.call(G,F)})||o.multiFilter(E,o.grep(this,function(F){return F.nodeType===1})),"filter",E)},closest:function(E){var G=o.expr.match.POS.test(E)?o(E):null,F=0;return this.map(function(){var H=this;while(H&&H.ownerDocument){if(G?G.index(H)>-1:o(H).is(E)){o.data(H,"closest",F);return H}H=H.parentNode;F++}})},not:function(E){if(typeof E==="string"){if(f.test(E)){return this.pushStack(o.multiFilter(E,this,true),"not",E)}else{E=o.multiFilter(E,this)}}var F=E.length&&E[E.length-1]!==g&&!E.nodeType;return this.filter(function(){return F?o.inArray(this,E)<0:this!=E})},add:function(E){return this.pushStack(o.unique(o.merge(this.get(),typeof E==="string"?o(E):o.makeArray(E))))},is:function(E){return !!E&&o.multiFilter(E,this).length>0},hasClass:function(E){return !!E&&this.is("."+E)},val:function(K){if(K===g){var E=this[0];if(E){if(o.nodeName(E,"option")){return(E.attributes.value||{}).specified?E.value:E.text}if(o.nodeName(E,"select")){var I=E.selectedIndex,L=[],M=E.options,H=E.type=="select-one";if(I<0){return null}for(var F=H?I:0,J=H?I+1:M.length;F<J;F++){var G=M[F];if(G.selected){K=o(G).val();if(H){return K}L.push(K)}}return L}return(E.value||"").replace(/\r/g,"")}return g}if(typeof K==="number"){K+=""}return this.each(function(){if(this.nodeType!=1){return}if(o.isArray(K)&&/radio|checkbox/.test(this.type)){this.checked=(o.inArray(this.value,K)>=0||o.inArray(this.name,K)>=0)}else{if(o.nodeName(this,"select")){var N=o.makeArray(K);o("option",this).each(function(){this.selected=(o.inArray(this.value,N)>=0||o.inArray(this.text,N)>=0)});if(!N.length){this.selectedIndex=-1}}else{this.value=K}}})},html:function(E){return E===g?(this[0]?this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g,""):null):this.empty().append(E)},replaceWith:function(E){return this.after(E).remove()},eq:function(E){return this.slice(E,+E+1)},slice:function(){return this.pushStack(Array.prototype.slice.apply(this,arguments),"slice",Array.prototype.slice.call(arguments).join(","))},map:function(E){return this.pushStack(o.map(this,function(G,F){return E.call(G,F,G)}))},andSelf:function(){return this.add(this.prevObject)},domManip:function(J,M,L){if(this[0]){var I=(this[0].ownerDocument||this[0]).createDocumentFragment(),F=o.clean(J,(this[0].ownerDocument||this[0]),I),H=I.firstChild;if(H){for(var G=0,E=this.length;G<E;G++){L.call(K(this[G],H),this.length>1||G>0?I.cloneNode(true):I)}}if(F){o.each(F,z)}}return this;function K(N,O){return M&&o.nodeName(N,"table")&&o.nodeName(O,"tr")?(N.getElementsByTagName("tbody")[0]||N.appendChild(N.ownerDocument.createElement("tbody"))):N}}};o.fn.init.prototype=o.fn;function z(E,F){if(F.src){o.ajax({url:F.src,async:false,dataType:"script"})}else{o.globalEval(F.text||F.textContent||F.innerHTML||"")}if(F.parentNode){F.parentNode.removeChild(F)}}function e(){return +new Date}o.extend=o.fn.extend=function(){var J=arguments[0]||{},H=1,I=arguments.length,E=false,G;if(typeof J==="boolean"){E=J;J=arguments[1]||{};H=2}if(typeof J!=="object"&&!o.isFunction(J)){J={}}if(I==H){J=this;--H}for(;H<I;H++){if((G=arguments[H])!=null){for(var F in G){var K=J[F],L=G[F];if(J===L){continue}if(E&&L&&typeof L==="object"&&!L.nodeType){J[F]=o.extend(E,K||(L.length!=null?[]:{}),L)}else{if(L!==g){J[F]=L}}}}}return J};var b=/z-?index|font-?weight|opacity|zoom|line-?height/i,q=document.defaultView||{},s=Object.prototype.toString;o.extend({noConflict:function(E){l.$=p;if(E){l.jQuery=y}return o},isFunction:function(E){return s.call(E)==="[object Function]"},isArray:function(E){return s.call(E)==="[object Array]"},isXMLDoc:function(E){return E.nodeType===9&&E.documentElement.nodeName!=="HTML"||!!E.ownerDocument&&o.isXMLDoc(E.ownerDocument)},globalEval:function(G){if(G&&/\S/.test(G)){var F=document.getElementsByTagName("head")[0]||document.documentElement,E=document.createElement("script");E.type="text/javascript";if(o.support.scriptEval){E.appendChild(document.createTextNode(G))}else{E.text=G}F.insertBefore(E,F.firstChild);F.removeChild(E)}},nodeName:function(F,E){return F.nodeName&&F.nodeName.toUpperCase()==E.toUpperCase()},each:function(G,K,F){var E,H=0,I=G.length;if(F){if(I===g){for(E in G){if(K.apply(G[E],F)===false){break}}}else{for(;H<I;){if(K.apply(G[H++],F)===false){break}}}}else{if(I===g){for(E in G){if(K.call(G[E],E,G[E])===false){break}}}else{for(var J=G[0];H<I&&K.call(J,H,J)!==false;J=G[++H]){}}}return G},prop:function(H,I,G,F,E){if(o.isFunction(I)){I=I.call(H,F)}return typeof I==="number"&&G=="curCSS"&&!b.test(E)?I+"px":I},className:{add:function(E,F){o.each((F||"").split(/\s+/),function(G,H){if(E.nodeType==1&&!o.className.has(E.className,H)){E.className+=(E.className?" ":"")+H}})},remove:function(E,F){if(E.nodeType==1){E.className=F!==g?o.grep(E.className.split(/\s+/),function(G){return !o.className.has(F,G)}).join(" "):""}},has:function(F,E){return F&&o.inArray(E,(F.className||F).toString().split(/\s+/))>-1}},swap:function(H,G,I){var E={};for(var F in G){E[F]=H.style[F];H.style[F]=G[F]}I.call(H);for(var F in G){H.style[F]=E[F]}},css:function(H,F,J,E){if(F=="width"||F=="height"){var L,G={position:"absolute",visibility:"hidden",display:"block"},K=F=="width"?["Left","Right"]:["Top","Bottom"];function I(){L=F=="width"?H.offsetWidth:H.offsetHeight;if(E==="border"){return}o.each(K,function(){if(!E){L-=parseFloat(o.curCSS(H,"padding"+this,true))||0}if(E==="margin"){L+=parseFloat(o.curCSS(H,"margin"+this,true))||0}else{L-=parseFloat(o.curCSS(H,"border"+this+"Width",true))||0}})}if(H.offsetWidth!==0){I()}else{o.swap(H,G,I)}return Math.max(0,Math.round(L))}return o.curCSS(H,F,J)},curCSS:function(I,F,G){var L,E=I.style;if(F=="opacity"&&!o.support.opacity){L=o.attr(E,"opacity");return L==""?"1":L}if(F.match(/float/i)){F=w}if(!G&&E&&E[F]){L=E[F]}else{if(q.getComputedStyle){if(F.match(/float/i)){F="float"}F=F.replace(/([A-Z])/g,"-$1").toLowerCase();var M=q.getComputedStyle(I,null);if(M){L=M.getPropertyValue(F)}if(F=="opacity"&&L==""){L="1"}}else{if(I.currentStyle){var J=F.replace(/\-(\w)/g,function(N,O){return O.toUpperCase()});L=I.currentStyle[F]||I.currentStyle[J];if(!/^\d+(px)?$/i.test(L)&&/^\d/.test(L)){var H=E.left,K=I.runtimeStyle.left;I.runtimeStyle.left=I.currentStyle.left;E.left=L||0;L=E.pixelLeft+"px";E.left=H;I.runtimeStyle.left=K}}}}return L},clean:function(F,K,I){K=K||document;if(typeof K.createElement==="undefined"){K=K.ownerDocument||K[0]
0

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 :)

0

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

0

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>
0

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

0

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.

0

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)?

This topic has been dead for over six months. 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.