Email form problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomE
Forum Newbie
Posts: 19
Joined: Tue Jul 12, 2005 7:26 am
Location: Liverpool

Email form problem

Post by tomE »

Here is my email submission form

Code: Select all

<?
$name=$_POSTї'name'];
$email=$_POSTї'email'];
$comments=$_POSTї'comments'];
$to=&quote;me@me.co.uk&quote;;
$message=&quote;$name just filled in your comments form. They said:\n$comments\n\nTheir e-mail address was: $email&quote;;
if(mail($to,&quote;Comments From Your Site&quote;,$message,&quote;From: $email\n&quote;)) {
echo &quote;Thanks for your comments.&quote;;
} else {
echo &quote;There was a problem sending the mail. Please check that you filled in the form correctly.&quote;;
}
?>
I then get the following errors

Code: Select all

Notice: Undefined index: name in c:\inetpub\wwwroot\PHP\mail.php on line 2

Notice: Undefined index: email in c:\inetpub\wwwroot\PHP\mail.php on line 3

Notice: Undefined index: comments in c:\inetpub\wwwroot\PHP\mail.php on line 4
Thanks for your comments.
Also the HTML contains a javascript function to validate the email address, do i need to
add anything to the php script to enable it as it only seems to work on its own as HTML i.e.
<form type=submit action=post> etc..
but not when the PHP is used to replace the action
i.e.<form type=submit action=PHP/mail.php>...


here is the full form

Code: Select all

&lt;!DOCTYPE HTML PUBLIC &quote;-//W3C//DTD HTML 4.01 Transitional//EN&quote;
&quote;http://www.w3.org/TR/html4/loose.dtd&quote;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quote;Content-Type&quote; content=&quote;text/html; charset=iso-8859-1&quote;&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;SCRIPT LANGUAGE=&quote;JavaScript&quote;&gt;


&lt;!-- Begin
function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) &lt; &gt; @ , ; : \ &quote; . &#1111; ] */

var specialChars=&quote;\\(\\)&gt;&lt;@,;:\\\\\\\&quote;\\.\\&#1111;\\]&quote;;

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars=&quote;\&#1111;^\\s&quote; + specialChars + &quote;\]&quote;;

/* The following pattern applies if the &quote;user&quote; is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. &quote;jiminy cricket&quote;@disney.com
is a legal e-mail address. */

var quotedUser=&quote;(\&quote;&#1111;^\&quote;]*\&quote;)&quote;;

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@&#1111;123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\&#1111;(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word=&quote;(&quote; + atom + &quote;|&quote; + quotedUser + &quote;)&quote;;

// The following pattern describes the structure of the user

var userPat=new RegExp(&quote;^&quote; + word + &quote;(\\.&quote; + word + &quote;)*$&quote;);

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp(&quote;^&quote; + atom + &quote;(\\.&quote; + atom +&quote;)*$&quote;);

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert(&quote;Email address seems incorrect (check @ and .'s)&quote;);
return false;
}
var user=matchArray&#1111;1];
var domain=matchArray&#1111;2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i&lt;user.length; i++) {
if (user.charCodeAt(i)&gt;127) {
alert(&quote;Ths username contains invalid characters.&quote;);
return false;
   }
}
for (i=0; i&lt;domain.length; i++) {
if (domain.charCodeAt(i)&gt;127) {
alert(&quote;Ths domain name contains invalid characters.&quote;);
return false;
   }
}

// See if &quote;user&quote; is valid 

if (user.match(userPat)==null) {

// user is not valid

alert(&quote;The username doesn't seem to be valid.&quote;);
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i&lt;=4;i++) {
if (IPArray&#1111;i]&gt;255) {
alert(&quote;Destination IP address is invalid!&quote;);
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp(&quote;^&quote; + atom + &quote;$&quote;);
var domArr=domain.split(&quote;.&quote;);
var len=domArr.length;
for (i=0;i&lt;len;i++) {
if (domArr&#1111;i].search(atomPat)==-1) {
alert(&quote;The domain name does not seem to be valid.&quote;);
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD &amp;&amp; domArr&#1111;domArr.length-1].length!=2 &amp;&amp; 
domArr&#1111;domArr.length-1].search(knownDomsPat)==-1) {
alert(&quote;The address must end in a well-known domain or two letter &quote; + &quote;country.&quote;);
return false;
}

// Make sure there's a host name preceding the domain.

if (len&lt;2) {
alert(&quote;This address is missing a hostname!&quote;);
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

//  End --&gt;
&lt;/script&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;form name=emailform onSubmit=&quote;return emailCheck(this.email.value)&quote; action=&quote;PHP/mail.php&quote;&gt;&lt;br&gt;
Your Name: &lt;input type=&quote;text&quote; name=&quote;name&quote; id=&quote;name&quote;&gt;&lt;br&gt;

Email:  &lt;input type=text name=&quote;email&quote; id=&quote;email&quote;&gt;&lt;br&gt;&lt;br&gt;
Comments&lt;br&gt;
&lt;textarea name=&quote;comments&quote; id=&quote;comments&quote;&gt;&lt;/textarea&gt;&lt;br&gt;&lt;br&gt;
&lt;br&gt;
&lt;input type=submit  value=&quote;Submit&quote;&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Change your error reporting:

Code: Select all

error_reporting(0);
http://us2.php.net/errorfunc
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

well you should generally code with error_reporting(E_ALL);

i didnt look at your code but use stuff like

Code: Select all

if (false !== isset($arr['key']))
{
}
that will keep those nasty errors away
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

thats an ugly way of doing it

Code: Select all

$key = !empty($arr['key']) ? $arr['key'] : '';
Post Reply