Help - Search - Members - Calendar
Full Version: Contact Me Script Trouble
Weborum Webmaster Forum > Web Page Design > PHP
CBrown
Hey guys,

I am having some trouble with Timo's "Web Mailer" script that was in the tutorials section. I have no idea how to work PHP, so go ahead and laugh when you see what I am trying to do. Well, here is what I have;
CODE

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<script language="JavaScript" type="text/javascript">
function check(form){

if (form.name.value == "") {
  alert("Please enter your name.");
  form.name.focus();
  return false;
}
else if (form.email.value == "") {
  alert("Please enter your email.");
  form.email.focus();
  return false;
}
else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value))){
  alert("Please enter a valid email address.");
  form.email.focus();
  return false;
  }
else if (form.comment.value == "") {
  alert("Please enter a comment/question.");
  form.comment.focus();
  return false;
}
else{
return true;
}
}

</script>

<?php

function mailcheck($emailadr){
return eregi("^[a-zA-Z0-9_\.-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$", $emailadr);
}

if (!isset($_POST['submit']))
#if submit doesn't have a value
{
?>
<h3>Feel free to contact me.</h3>
<form method="post" name="emailform" action="<?php echo $_SERVER['SCRIPT_NAME']?>" onSubmit="return check(this);">
<p>Name : <input type="text" name="name" /></p>
<p>Email : <input type="text" name="email" /></p><br />
<p>Comments/Questions : <br /><textarea name="comment" rows="10" cols="30"></textarea></p>
<p><input type="submit" name="submit" value="Submit" /></p>
</form>

<?php
}
else
{
$name = $_POST['name'];
$email = $_POST['email'];
$comment = stripslashes($_POST['comment']);
$to  = "EMAIL LEFT OUT ";

if ((mailcheck($_POST['email']))||($comment!=NULL)||($name!=NULL))
#check if a real email address, if it returns true it sends the email
#also checks to make sure the comment and name fields aren't empty
{
# subject
$subject = "Mail from site.";

# message
$message = '
<html>
<head>
<title>Mail from site</title>
</head>
<body>
<p>The email contains user comments.</p>
<br />
Name of person : '.$name.'<br /> #I have it displaying the name
Email of person : '.$email.'<br />#here the email
Comment/Question left:<br />
<p>'.$comment.'</p>#here the comment
<br />
              #it uses html so feel free to format to your pleasure
</body>
</html>
';

$headers  = "MIME-Version: 1.0\r\n"; #headers
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";


$headers .= "From: $email>\r\n"; #The sender's email
#Will show up in your box from their email address

#mail it
mail($to, $subject, $message, $headers);
echo 'Thank you for sending mail.';
}
else {
echo 'Sorry, one of more or your fields is invalid.';}
}

?>
</body>
</html>

Anyways, it dosen't work. I am sure something is wrong. I am pretty sure that the php code has to be saved externally, but I don't know how to do that, or how to reference it. If someone could tell me what I have done wrong, that would be much appreciated.

thanks in advanced,
Chris
Waleed
what extension did you save it in? it has to be .php for the code to work....I don't know if having the mailer stuff in the same file effects anything but you could always try...?

take out the PHP code and save it in a new file and include it in the email page? don't know if it would work or not....

CODE
<?php include('mailthing.html'); ?>


can't be of much help because I too have just started using PHP....
Joe
Try this Chris;

PHP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
</head>

<body>

<script type="text/javascript">
function check(form){

if (form.name.value == "") {
  alert("Please enter your name.");
  form.name.focus();
  return false;
}
else if (form.email.value == "") {
  alert("Please enter your email.");
  form.email.focus();
  return false;
}
else if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value))){
  alert("Please enter a valid email address.");
  form.email.focus();
  return false;
  }
else if (form.comment.value == "") {
  alert("Please enter a comment/question.");
  form.comment.focus();
  return false;
}
else{
return true;
}
}
</script>

<?php
function mailcheck($emailadr){
return
eregi("^[a-zA-Z0-9_\.-]+@[a-zA-Z0-9\.-]+\.[a-zA-Z]{2,4}$", $emailadr);
}

if (!isset(
$_POST['submit']))
{
?>

<h3>Feel free to contact me.</h3>

<form method="post" action="<?=$_SERVER['PHP_SELF'] ?>" onSubmit="return check(this);">

<p><label for="name">Name : </label><input type="text" name="name" id="name" /></p>

<p><label for="email">Email : </label><input type="text" name="email" id="email" /></p>

<p><label for="comments">Comments/Questions :</label><br />

<textarea name="comment" id="comments" rows="10" cols="30"></textarea></p>

<p><input type="submit" name="submit" value="submit" /> <input type="reset" value="reset" /></p>

</form>

<?php
} else {

$name = $_POST['name'];
$email = $_POST['email'];
$comment = stripslashes($_POST['comment']);

if ((
mailcheck($email)) || ($comment!=NULL) || ($name!=NULL)) {

$to  = "Your Email Address";
$subject = "Mail from site.";
$message = '
<html>
<head>
    <title>'
.$subject.'</title>
</head>
<body>

<p>The email contains user comments.</p>

<p>Name of person : '
.$name.'</p>

<p>Email of person : '
.$email.'</p>

<p>Comment/Question left : <br />
'
.$comment.'</p>#here the comment</p>

</body>
</html>
'
;

$headers  = "MIME-Version: 1.0\r\n"; #headers
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: $email>\r\n"; #The sender's email

mail($to, $subject, $message, $headers);
echo
'Thank you for sending mail.';
}
else {
echo
'Sorry, one of more or your fields is invalid.';
}
}

?>
</body>
</html>
Timo
I think I had a problem with the script that I didn't realize I had... I'm about 95% sure I edited the script to fix that error though. It's probably something on your side...

Try Joe's script though, it's probably much better than my old script. If I find the time I'll update it with some advances. Sorry for the troubles.

-Tim
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.