Click to See Complete Forum and Search --> : PHP error - Please help my small brain hurts


MrTbone
04-06-2007, 05:15 PM
Yep another newbie to PHP ..sorry :confused:

I have read through quite a few of the threads on this forum regarding PHP forms and how to make them work.This has got me to the stage I am at now...sad eh? I would appreciate any help all you experts out there can offer. My site has a form which when filled out and the submit button hit gives me the following error message , under which appears the message for the user.

#!/usr/local/bin/php
Notice: Undefined variable: msg in e:\domains\s\mysite.co.uk\user\htdocs\contact.php on line 11

my PHP script is below, I have changed my actual email in the script to myemail@somewhere.com and in the above error text I have replaced my actual domain with mysite.co.uk

thanks in advance to anybody taking the time to read this I'm sure from looking at all the other threads you must be tired of helping us newbies. Oneday I hope to be answering stuff for others....oh hum I can dream can't I

my PHP script

#!/usr/local/bin/php

<?php
/* set up to handle either post or get form variables */
if (count($_POST)>0) $response = $_POST;
else $response = $_GET;

/* iterate through form variables and display them as key: value pairs */
foreach ($response as $key => $val)
{
$msg .= "$key: $val";
$msg .= "\r\n";
}
/* strip slashes from characters like apostrophe */
$msg = stripslashes($msg);

/* send the message */
mail("myemail@somewhere.com", "form response", $msg);
?>

<html>
<head>
<title>survey handler</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#339900" text="#000000">
<p>
<center>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

<p><font color="#FFCC99" size="4" face="Comic Sans MS"><b><font size="5">Thank
you for your enquiry, we will be in touch.</font></b></font></p>
<p><font size="5"><b><font color="#FFCC99" face="Comic Sans MS">Have a nice
day!</font></b> </font></p>
</center>
</p>
</body>
</html>
many thanks
MrTbone

NogDog
04-06-2007, 09:51 PM
The notice is occurring because the very first time the line $msg .= "$key: $val"; is executed, the variable $msg is not yet set, but you are trying to append a value to it. Therefore the PHP parser is throwing a notice saying, "Are you sure you meant to do that?" To avoid it, you can simply throw in the following line before you start that foreach loop:

$msg = '';

MrTbone
04-07-2007, 05:44 AM
NogDog...

My Hero!......wish I could get ny head round this stuff

the error message has now dissappeared and is loading the "Thank You" page but I am not receiving the contact email..?

any ideas?

:o

boxxertrumps
04-07-2007, 11:46 AM
Where is the mail server your using located?
The default is localhost.

MrTbone
04-07-2007, 12:00 PM
Hi Boxxertrumps :)

errr......again escuse my ignorance , what do you mean where is it located?...somewhere in space I guess....DOH! see I really am a newbie

can you elaborate for me...sorry !

NogDog
04-07-2007, 12:19 PM
You likely need to add a valid "From:" email address to the additional headers, and that from address will have to be a valid email address on the server where your script is running. See the "Sending mail with extra headers" example on the mail() page (http://www.php.net/manual/en/function.mail.php).

boxxertrumps
04-07-2007, 12:49 PM
There's some info on stmp here: http://ca.php.net/mail
Along with the rest of the mail() info.

MrTbone
04-07-2007, 01:49 PM
NoDog & Boxxertrumps....Thanks for all the input guys I'm not sure I fully understand all the stuff you are talking about but I did manage to get the page loading properly and got a mail repsonse from my form so I must have taken some of it in.

I'll keep practicing coz its brill when it all works init..!!

I'm sure I will be back here with some more questions real soon..! :D

thanks again