Here's my code:
<?php function mySpecialForm () {
$form = '<form action="'.$_SERVER['PHP_SELF'].'" method="post" >
<span class="heading" ><img src="images/webbilder/kontaktimage.gif" />
</span><br /><br /><span class="felter">Felter merket * må fylles ut</span><br /><br /> <span class="skjema">Firstname*</span><br />
<input name="navn" type="text" class="textfelt" size="40" maxlength="40" value="'.$_POST['navn'].'" /><br /><br />
<span class="skjema">Lastname*</span><br />
<input name="etternavn" type="text" class="textfelt" size="40" maxlength="40" value="'.$_POST['etternavn'].'" /><br /><br />
<span class="skjema">Telephonenumber</span><br />
<input name="mobil" type="text" class="textfelt" size="20" maxlength="8" value="'.$_POST['mobil'].'" /><br /><br />
<span class="skjema">e-mailaddress*</span><br />
<input name="email" type="text" class="textfelt" size="40" maxlength="40" value="'.$_POST['email'].'" /><br /><br />
<span class="skjema">Subject</span><br />
<input name="emne" type="text" class="textfelt" size="40" value="'.$_POST['emne'].'" maxlength="40" /><br /><br />
<span class="skjema">Your message*</span><br />
<textarea name="melding" cols="70" rows="10" class="textfelt">'.$_POST['melding'].'</textarea> <br /><br />
<input name="submit" type="submit" value="send" />
<input name="reset" type="reset" value="slett" />
</form> ' . "\n";
return $form;} if($_POST['submit']=="send") {
$errcount = 0; $error = "<span style='color:red'>";
//check if someone has filled out a form //validate name if(preg_match('/^[a-zæøåÆØÅ_-]{2,}$/i', $_POST['navn'])){
$navn = $_POST['navn'];
}
else {
$error .= "You didn't write a name!<br />";
$errcount++;
} //check lastname //validate lastname
if(preg_match('/^[a-zæøåÆØÅ_-]{2,}$/i', $_POST['etternavn'])){
$etternavn = $_POST['etternavn'];
}
else {
$error .= "You didn't write your lastname!<br />";
$errcount++;
} //validate telephonenumber
if(preg_match('/^[0-9_-]{8,}$/i', $_POST['mobil'])){
$mobil = $_POST['mobil'];
}
else {
$error .= "You didn't write a correct telephonenumber!<br />";
$errcount++;
} //check correct email
if(preg_match('/^[\w]+[-\w._]*@[\w]+[-\w]+(\.[\w]+[-\w]+)*\.[\w]{2,6}$/ ', $_POST['email'])){ $epost = $_POST['email'];
}
else {
$error .= "You didn't write a valid e-mail!<br />";
$errcount++;
}
if (!empty($_POST['melding'])) {
$melding = $_POST['melding'];
}
else {
$error .= "You didn't write a message!</span><br />";
$errcount++;
}
//Check for faults //if there's a fault, stop the script and preview the following
if ($errcount !=0) {
echo "<p>Sorry, there is $errcount faults in your form. Press your <b>back</b> button to correct these:</p>" . $error;
echo mySpecialForm();
} else {
$emne = $_POST['emne'];
$mailheaders = "From: " .$navn ." " .$etternavn ." <". $epost . ">\n";
$mailheaders .= "Reply-To: <". $epost . ">\n";
$to = 'youremail@somewhere.com';
$melding .= $_POST['melding'];//I put the message at the bottom
$melding = "Fra: $navn $etternavn\nTelefon: $mobil\n\nMelding:\n$melding";
//Send Mail
mail($to, $emne, $melding, $mailheaders,'youremailstuffhere');
header ("Location: thankyou.htm");
} echo mySpecialForm();?>
Aaaargh! 😕 This code doubles up my form when downloaded...
I need to now how the form only previews once (not twice) in the same page..
It all happened when I tried to put the errormessage (should be previewed if an error in the validation..) at the top of my form... I made the error come in a variable with the form inside of it...
Also, I think the validation happens before I click the submit button..., which makes the user go 😕 haven't been able to fill out enything yet.. 😃
Anyone..??
Anyone..??