Re: Why is content of $name empty?



Now I have done a bitt more.. The hole application look like this:
At bottom I have some questons.. (This is just a beginning. I shall rewrite it and use functions)
I do need a tip to how I can check the headers..

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";>
<html xmlns="http://www.w3.org/1999/xhtml"; xml:lang="en" lang="en">
<head>
<title>New document</title>
<meta name="generator" content="TSW phpCoder 2008" />
</head>
<body>

<form action="index.php" method="post">
<p>Name:<br /><input type="text" name="name" size="50" maxlength="50" value=" . <?php $_POST['name']; ?> ." /></p>
<p>E-mail:<br /><input type="text" name="email" size="50" maxlength="80" /> </p>
<p>Subject:<br /><input type="text" name="subject" size="50" maxlength="50" /></p>
<p>Message:<br /><textarea cols="50" rows="10" name="message"></textarea></p>
<p><input type="reset" value="reset"/> <input type="submit" name="prayer_request" value="Send" /></p>
</form>

<?php
if(isset($_POST['prayer_request'])){

// Save form content in variables
$name = $_POST['name'];
$mail = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];



// Check if any field are empty
if(empty($name)){
echo "<p>Name have to be written!</p>";
return false;
}
if(empty($mail)){
echo "<p>Have you forgot to write your email address?</p>";
return false;
}
if(empty($subject)){
echo "<p>You need to write a subject for your message!</p>";
return false;
}
if(empty($message)){
echo "<p>Please write down a message to us!</p>";
return false;
}

// Check the fields that enough characters are written
// in each of them
if(strlen($name) < 2){
echo "You need at least two characters in the Name field";
return false;
}
if(strlen($mail) < 8){
echo "Please enter your email address!";
return false;
}
if(strlen($subject) < 3){
echo "You have not write a subject for your message. Use at least 3 letters!";
return false;
}
if(strlen($message) < 10){
echo "Please write down a message to us. Use at least 10 characters in it!";
return false;
}


// Strip HTML codes from the input fields
$name = strip_tags($name);
$mail = strip_tags($mail);
$subject = strip_tags($subject);
$message = strip_tags($message);

// Check the Email address.
// Taken from article at
// http://www.totallyphp.co.uk/code/validate_an_email_address_using_regular_expressions.htm
if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $mail)) {
// echo "Valid email address.";
$mail;
}
else {
echo "Invalid email address.";
return false;
}

// Convert HTML characters to entity
// if the message still contains it
$name = htmlentities($name);
$mail = htmlentities($mail);
$subject = htmlentities($subject);
$message = htmlentities($message);

// If message contains æøå/ÆØÅ that is translated above
// by htmlentities, then translate the letter back
$name = str_replace("&AElig;", "Æ", $name);
$name = str_replace("&Oslash;", "Ø", $name);
$name = str_replace("&Aring;", "Å", $name);
$name = str_replace("&aelig;", "æ", $name);
$name = str_replace("&oslash;", "ø", $name);
$name = str_replace("&aring;", "å", $name);

$subject = str_replace("&AElig;", "Æ", $subject);
$subject = str_replace("&Oslash;", "Ø", $subject);
$subject = str_replace("&Aring;", "Å", $subject);
$subject = str_replace("&aelig;", "æ", $subject);
$subject = str_replace("&oslash;", "ø", $subject);
$subject = str_replace("&aring;", "å", $subject);

$message = str_replace("&AElig;", "Æ", $message);
$message = str_replace("&Oslash;", "Ø", $message);
$message = str_replace("&Aring;", "Å", $message);
$message = str_replace("&aelig;", "æ", $message);
$message = str_replace("&oslash;", "ø", $message);
$message = str_replace("&aring;", "å", $message);




// Get the content in ma.txt file
$myFile = "ma.txt";
$handle = fopen($myFile, 'r');
$Data = fread($handle, 99999999);
fclose($handle);

// Replace \r\n with ,
$Data = str_replace("\r\n",",", $Data);

// Send it
$to = "Karl@localhost, ";
$from = "From: $mail";
$subject = "$subject";
$body = "From: $name\nEmail: $from\nSubject: $subject\nMessage:\n$message";
$headers = 'Content-type: text/plain; charset=utf-8' . "\r\n";
$headers .= "$from \n";

// Example taken from PHP Manual

$arr = array("$Data");
reset($arr);
// Take all values in $Data that is Saved in $arr and
// save them in $value
while (list(,$value) = each($arr)) {

// Save contents in $headers
$headers .= "Bcc: $value";
}
//$header .= "Karl@localhost";
mail($to, $subject, $body, $headers);


echo "<p>Message is sent</p>";

}
?>

<h3>Feature of this PHP Application</h3>
<p>
<ul>
<li>It collect message from users and send it to different e-mail addresses that is written in a regular textfile</li>
<li>It check if any fields in the HTML form is empty</li>
<li>It check if a minimum of characters is written in each field</li>
<li>It check if the email address given, have correct syntax</li>
<li>It translate HTML tags into entity code</li>
<li>It support UTF-8 characters</li>
<li>It support &AElig;&Oslash;&Aring; - &aelig;&oslash;&aring;. (Norwegian language)</li>
<li>It support unlimit number of emails that can receive the content from the form!</li
<li>No need for Databases!</li>
</ul>
</p>
</body>
</html>

Do you know about a web pages were they talk about how to check headers for error? Or can you give me a tip about how to start?

Thanks for all your help! I am very thankful for ut. This is just a learning project and when it is done I will use it on a private server not connected to the web... Well, if I can make it good enough, I give it away as freeware under GNU of course... But I guess I have to learn a lot more before that is actually!

Kind Regards,
Karl-Arne
.