I have just finished the Email Form Submit tutorial located in the top of the
Php forum next to Rakuli's Tutorial. I have uploaded the email submit form to a server and were the person is meant to be filling in the textfields in the form and so on i can see the code from the form itself inside the textfields, at first i thought the problem was the way i had copied the code, but the creator of the tutorial sent me the
Php file and its still the same. So just imagine you wanted to fill in your name in the name section of the form i carnt because the text field itself is filled with the code.
Here's the origanal code from the origanal
Php file.
Is there anyone who can help me with a email form submit script, i have tried countless sites on the net and they all seem a little faulty hear and there.
If anyone has a fix 4 the code below it would be great.
- PHP: Select all
<?php
/****** PHP Form Mail *****/
// declare and initialize some variables
$name=$email=$subject=$message=$error_message='';
$invalid_fields=array();
$required_fields=array('name','email','message');
$validated=array();
$to_address='blub@bla.com'; // your email address goes here
// validate $_POST array
if(count($_POST)>0) { // was something posted?
foreach($_POST as $key=>$value) { // loop through the $_POST array
if(in_array($key,$required_fields)&&$value=='') { // check if a required field is empty
// add that field to the $invalid_fields array
array_push($invalid_fields,$key);
// and append the error message to the $error_message variable
$error_message.='<p>Please enter a'.(preg_match('/^[aeiouy]/',$key)?'n':'').' '.$key.'.</p>';
} else {
switch($key) {
case 'email': // validate email address format
if(!preg_match('/^([A-Z0-9._%-]+)@([A-Z0-9.-]+)\.([A-Z]{2,6})$/i',$value)) {
array_push($invalid_fields,'email');
$error_message.='<p>Please enter a valid email.</p>';
}
break;
}
}
// field is not in the $invalid_fields array?
if(!in_array($key,$invalid_fields)) {
// copy it to the $validated array
$validated[$key]=htmlspecialchars($value);
}
}
} else { // make everything invalid so that the form is outputted and not the thankyou message
$invalid_fields=$required_fields;
}
?>
<!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>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>form mail</title>
<style type="text/css" media="all">
* { margin:0;padding:0; }
body { font:600 12px/22px verdana;padding:50px; }
.box, textarea, .button { border:2px solid #6699CC;font:inherit;color:inherit; }
.box { width:250px; }
textarea { width:300px;height:250px; }
.error { color:#DD1100; }
</style>
</head>
<body>
<div class="contact">
<h1>Contact Us</h1>
<?php
if(count($invalid_fields)<=0) { // send message / thank user
$formatted_message='When: '.date('r').'
Who: '.$name.' ('.$email.')
With What: '.$_SERVER['HTTP_USER_AGENT'].'
Message: '.$message;
//mail($to_address,$subject,$formatted_message);
echo($formatted_message);
?>
<p>Your message was successfully delivered.</p>
<?php } else { ?>
<p>Please fill out the form to contact us. Required fields are marked with a star [*].</p>
<?php echo(($error_message!=''?'<div class="error">'.$error_message.'</div>':'')); ?>
<form name="contact" action="form_mail.php" method="post">
<p>Name:* <input class="box" type="text" name="name" value="<?php echo($validated['name']); ?>" /></p>
<p>E-Mail:* <input class="box" type="text" name="email" value="<?php echo($validated['email']); ?>" /></p>
<p>Subject: <input class="box" type="text" name="subject" value="<?php echo($validated['subject']); ?>" /></p>
<p>Message:*</p><textarea name="message"><?php echo($validated['message']); ?></textarea>
<p><input class="button" type="submit" action="submit" value="Send" />
</form>
<?php } ?>
</div>
</body>
</html>