-1

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>
CoDINGinDARK
  • 244
  • 4
  • 16
  • 1
    It looks like you are using the assignment operator(=) instead of the equality operator (==) when checking the email. This causes the $_POST['email'] var to be assigned to an empty string. Just change the part that says `$_POST['email']=""` to `$_POST['email']==""` – Cave Johnson May 13 '15 at 17:13
  • sidenote: your mail may very well end up in spam and/or be marked as. No proper use of additional headers. – Funk Forty Niner May 13 '15 at 17:20
  • @Fred-ii- nope , I use xampp and is this the efficent way of writing the code, or can my code be improved ? – CoDINGinDARK May 13 '15 at 17:25
  • See the manual http://php.net/manual/en/function.mail.php and see Example #2. Plus, the way you have your POST arrays inside `mail()`, is prone to XSS injection; not good. – Funk Forty Niner May 13 '15 at 17:27

1 Answers1

1

Your error is here

if ( $_POST['email']="" || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ){

Replace it by :

if ( $_POST['email']=="" || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL) ){

(= replaced by ==)

Without it, your $_POST['email'] is an empty string.

Ulti
  • 588
  • 5
  • 18
  • 1
    I am extremely sorry, I study at home and I have nobody to point it out.Thank you, I promise next time I will be very causious and never spoil this community. – CoDINGinDARK May 13 '15 at 17:18