I have created a simple form validation, the PHP code when used inside the value
attribute to auto type the form, the name and he comments display properly expect the email
(input type="text").Have I written the code correctly or has my code has any code smells.
Edit: My question is not that, I don't receive a main it is that I get a value of 1 or the value gets cleared of.
PHP code :-
<?php
if($_SERVER['REQUEST_METHOD']=='POST') {
$error="";
if(strlen ( $_POST['name'] ) < 5 ) {
$error = "Please type more than 4 characters<br/>";
}
if ( $_POST['email']="" || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ){
$error.= "Please type a valid Email<br/>";
}
if(strlen($_POST['comment']) < 4){
$error.= "Please type more than 4 characters";
}
if( !empty($error) ){
$result = "<div class='alert alert-danger'>$error</div>";
}
else {
if( mail('jokersspirit@gmail.com','test message',"Name:".$_POST['name'].
"Email:".$_POST['email'].
"Comment:".$_POST['comment']) ){
$result = "<div class='alert alert-success'>Your form has been submitted</div>";
}
else{
$result = "<div class='alert alert-success'>Error occurred
while submitting your form please try again later</div>";
}
}
}
?>
HTML code : -
<form action="" method="post">
<label for="name">Your Name:</label>
<input type="text" class="form-control" name="name" id="name" placeholder="name" value="<?php echo isset($_POST['name'])?$_POST['name']:""; ?>">
<label for="email">Your Email:</label>
<input type="text" class="form-control" name="email" id="email" placeholder="email" value="<?php echo isset($_POST['email'])? $_POST['email']:""; ?>">
<label for="comment">Your Comments:</label>
<textarea name="comment" placeholder="comments" class="form-control" id="comment">
<?php echo isset($_POST['comment'])?$_POST['comment']:""; ?>
</textarea>
<input type="submit" class="btn btn-success btn-lg " value="Submit">
</form>