PDA

View Full Version : Contact form, slight help needed


Mark_W
07-24-2007, 11:42 AM
I had a contact form which when submitted would send you to one page to confirm that the email had been sent or another if there was a problem with sending the email (no comment or whatever). When the contact form was submitted successfully it would create a cookie, and then the user wouldn't be able to submit another message for I think it was 5 minutes.

However, over the past 2 days I have learnt that it is possible to have those 3 pages merged into one by the use of PHP.

I have made it possible to fill out the contact form and send it. However I'm running into problems with the cookie, because the cookie is being set towards the bottom of the code it returns the following error.

Warning: Cannot modify header information - headers already sent by (output started at /home/****/public_html/newsite/contact.php:33) in /home/****/public_html/newsite/contact.php on line 110

The following is the code:

<?php

if (!$_POST['submit'])
{
?>
<div id="mcontenttop">Contact Mark</div>
<div id="mcontentcontact">

<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
<table>
<tr>
<td>Your Name</td>
<td><input name="name" size="25" /></td>
</tr>
<tr>
<td>Email Address</td>
<td><input name="email" size="25" /></td>
</tr>
<tr>
<td>Subject</td>
<td><input name="subject" size="25" /></td>
</tr>
<tr>
<td>Comments</td>
<td><textarea name="comments" cols="40" rows="10"></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="submit" type="submit" value="Send"/></td>
</tr>
</table>
</form>

</div>
<?php
}
else
{
if ($submit) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$comments = $_POST['comments'];
$ip = $_SERVER['REMOTE_ADDR'];


$recipient = "Mark@marksplayroom.co.uk";
$emailsubject = "Email sent from Contacts Page";

$message = "E-mail: $email \n";
$message .= "Subject: $subject \n";
$message .= "Message: $comments \n";
$message .= "IP Address: $ip";

$headers = "From: $name \n";
$headers .= "Reply-To: $email";

if($_COOKIE['sentmessage']!=1){

mail($recipient,$emailsubject,$message,$headers);
setcookie("sentmessage",1,time()+300); ///The Culprit

echo 'Message Sent <a href""> Go Back</a>';
}else{

echo 'You have already sent a message, please wait a few minutes before sending another, thank you';

}
}
}
?>

If anyone understands what I have just said then I commend you. If anyone's able to give me advice then that's even better.

Thanks,

Mark

davidj
07-24-2007, 12:34 PM
Warning: Cannot modify header information - headers already sent by

that errror points to an output then the header is sent again...

i can see by your code you are outputing this html on a condition...


<div id="mcontenttop">Contact Mark</div>
<div id="mcontentcontact">

<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
then you are echoing ...

echo 'You have already sent a message


the trick is to assign all output to variables inside the php then echo the variables inside the HTML

Mark_W
07-24-2007, 12:43 PM
Sorry David you've lost me there. Is it possible for an example? Not necessarily with my code, but just so I can understand what your saying.


Thank you

davidj
07-24-2007, 01:56 PM
ok

a php script comes in 2 guises

100% php where all the code is to be processed by the php engine (no HTML)
half and half where some HTML and PHP combine because you are going to output a valueif you want to run a php script and output a value using PHP and HTML

/// The wrong way ....

/////////
<?
$php stuff;
$php stuff;
$php stuff;

echo "php stuff";
?>
<html>

<table>
<tr>
<td>test test</td><td><?php echo "hello world"; ?></td>
</tr>
</table>

</html>


/// The right way ....

/////////
<?
$php stuff;
$php stuff;
$php stuff;

$var = "php stuff";
?>
<html>

<table>
<tr>
<td>test test</td><td><?php echo $var; ?></td>
</tr>
</table>

</html>


if you output inside the php then close the php then send headers to output HTML you going to get errors.

Best practice is never output using an echo within logic above the HTML. You have no control where it displays and your going to run into trouble

Mark_W
07-24-2007, 02:13 PM
Oh right yeah, I understand what your saying, I normally do do that, but for some reason I forgot on this page.

davidj
07-24-2007, 03:07 PM
you need a good memory to be a programmer and an elephant

Mark_W
07-24-2007, 03:26 PM
Well I can assure you I'm not a goldfish.

I think its purely because I copied the code from a previous contact form file I created from a tutorial and didn't bother to understand what bits meant and only changed the relevant coding.

Mark_W
07-24-2007, 07:58 PM
Ignore this post, I don't deserve to live.

carolinawebdesign
07-25-2007, 01:06 PM
Mark,
You are funny! I am reading this post after searching for "form" with hopes to understand them. Thanks for your ignorance, it will help me shed some of mine!

Mark_W
07-25-2007, 04:24 PM
To be honest I haven't even sorted out my problem, I decided to keep the form in the style I know how to do.

davidj
07-25-2007, 06:29 PM
do you not understand my explanation ?

are you still having problems

Mark_W
07-25-2007, 09:29 PM
I understand your explanation, at least I think I do, but I'm still getting the same problem.


The problem isn't anything to do with any of the echos but to do with the cookies, as when I remove the cookie code the form works well.

I thought maybe you meant that I need to put the set cookie code in a variable, but that obviously didn't work as the cookie was set before it was needed to.

I don't intend to annoy anyone, and maybe I've missed something obvious and its my stress that is shielding it from me.

davidj
07-25-2007, 09:30 PM
post the code

and the error

Mark_W
07-25-2007, 10:09 PM
Done it!!!

I needed to use the ob_start(); and ob_end_flush(); function

I apologise for not making myself clearer with my problem.

Thank you for your help anyway David.

davidj
07-25-2007, 10:12 PM
no probs

any time