Reputation: 15
I am trying to make an contact form for my website and I am using the following code. I don't know why the selector keeps reseting when the text fields don't.
Anyone know a soultion for this? Or what I am doing wrong.
( It resets when the error check is done, and fails )
<?php
session_start();
$php_self = $_SERVER['PHP_SELF'];
// on submit
if( isset($_POST[name]) && isset($_POST[email]) && isset($_POST[message]) && isset($_POST[captcha]) ){
$name = $_POST[name];
$email = $_POST[email];
$telefon = $_POST[telefon];
$option = $_POST[option];
$message = $_POST[message];
$captcha = $_POST[captcha];
$error = 0;
// name
if( $name == "" ){ $error ++; $error_name = "class='error'"; }
// email
if( $email == "" ){ $error ++; $error_email = "class='error'"; }
// message
if( $message == "" ){ $error ++; $error_message = "class='error'"; }
// captcha
if( $captcha == "" || $captcha != $_SESSION[captcha]){ $error ++; $error_captcha = "class='error'"; }
// no error, send email
if( $error == 0){
// your email address
$address = "[email protected]";
// email subject
$subject = "x , Nytt Meddelande \r\n";
// email content
$content = "Namn: $name \r\nE-postadress: $email \r\nTelefon Nr: $telefon \r\nKontakta mig via: $option \r\nMeddelande: $message";
// html email
$email_content = "".$subject."";
$email_content .= "";
$email_content .= $content;
$email_content .= "";
// send email
mail($address, $subject, $email_content, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n");
// reset variables
$name = ""; $email = ""; $message = "";
$mail_sent = 1;
}
}
// captcha
$num = rand(1, 20);
$num2 = rand(1, 9);
$verif = $num . " + " . $num2;
$_SESSION[captcha] = $num + $num2;
if( $mail_sent == 1 ){
echo "<div class=","sucess",">Ditt meddelande har skickats, vi återkommer så fort som möjligt.</div>";
} else {
echo "
<form action='".$php_self."' method='post'>
<p><span class='required'>* är obligatoriska fält.</span></p>
<p><strong>Namn:</strong> <span class='required'>*</span></p>
<input type='text' ".$error_name." name='name' value='".$name."'>
<p><strong>E-postadress:</strong> <span class='required'>*</span></p>
<input type='email' ".$error_email." name='email' value='".$email."'>
<p><strong>Telefon Nr:</strong></p>
<input type='telefon' name='telefon' value='".$telefon."'>
<p><strong>Kontakta mig via:</strong></p>
<select name='option'>
<option value='-'>-</option>
<option value='Telefon'>Telefon</option>
<option value='E-post'>E-post</option>
</select>
<p><strong>Meddelande:</strong> <span class='required'>*</span></p>
<textarea ".$error_message." name='message'>".$message."</textarea>
<p><strong>Hur mycket är ".$verif."?</strong> <span class='required'>*</span></p>
<input type='text' ".$error_captcha." name='captcha' value=''><br/>
<button>Skicka</button>
</form>";
}
?>
Upvotes: 0
Views: 37
Reputation: 2731
You have to add a check for each option
Tag like you do with the inputs and load from the $_POST value before...
$option = $_POST['option'];
And in the select
<select name='option'>
<option value='-'>-</option>
<option value='Telefon' ".($option == 'Telefon' ? "selected":"").">Telefon</option>
<option value='E-post' ".($option == 'E-post'? "selected" : "").">E-post</option>
</select>
Changed it to the inline string statement for the output string that the OP is generating...
Upvotes: 1