php - Form validatation and result on same html page -


i have form use on page client able send me messages. everythings ok , working. 1 thing missing every form errors , result (email typ error, first name missing) showing on new page. have on same page have notifications , result e.g "your message has been send". saw in pages forms input can highlitet on red if putted wrong etc. read can done either ajax or jquery honest not familaiar it. in solution using bootstrap , php. can me achieve that? below current code.

html (using bootstrap):

<form action="send_form_email.php" method="post" class="form-horizontal">               <div class="form-group">                 <label for="first_name" class="col-lg-2 control-label">first name</label>                 <div class="col-lg-10">                 <input type="text" class="form-control" id="first_name" name="first_name" placeholder="enter first name">                 </div>               </div><!-- end form group -->                 <div class="form-group">                 <label for="last_name" class="col-lg-2 control-label">last name</label>                 <div class="col-lg-10">                   <input type="text" class="form-control" id="last_name" name="last_name" placeholder="enter last name">                 </div>               </div><!-- end form group -->                <div class="form-group">                 <label for="email" class="col-lg-2 control-label">email</label>                 <div class="col-lg-10">                   <input type="text" class="form-control" id="email" name="email" placeholder="enter email address">                 </div>               </div><!-- end form group -->                <div class="form-group">                 <label for="telephone" class="col-lg-2 control-label">telephone</label>                 <div class="col-lg-10">                   <input type="text" class="form-control" id="telephone" name="telephone" placeholder="enter phone number">                 </div>               </div>                <div class="form-group">                 <label for="comments" class="col-lg-2 control-label">any message</label>                 <div class="col-lg-10">                   <textarea name="comments" id="comments" name="comments" class="form-control"                    cols="20" rows="10" placeholder="enter message"></textarea>                 </div>               </div>                 <div class="form-group">                 <div class="col-lg-10 col-lg-offset-2">                   <button type="submit" class="btn btn-primary">submit</button>                 </div>               </div>                </form> 

here's php handle email sending , form validation:

<?php if(isset($_post['email']))  {      // change 2 lines below     $email_to = "myemail@gmail.com";      $email_subject = "nowy formularz od osoby";       function died($error) {         // error code can go here         echo "we sorry, there error(s) found form submitted. ";         echo "these errors appear below.<br /><br />";         echo $error."<br /><br />";         echo "please go , fix these errorsbr /><br />";       echo '<input type="button" value="back" onclick="history.back();">';          die();     }      // validation expected data exists     if(!isset($_post['first_name']) ||         !isset($_post['last_name']) ||         !isset($_post['email']) ||         !isset($_post['telephone']) ||         !isset($_post['comments'])) {         died('we sorry, there appears problem form submitted.');            }      $first_name = $_post['first_name']; // required     $last_name = $_post['last_name']; // required     $email_from = $_post['email']; // required     $telephone = $_post['telephone']; // not required     $comments = $_post['comments']; // required      $error_message = "";     $email_exp = '/^[a-za-z0-9._%-]+@[a-za-z0-9.-]+\.[a-za-z]{2,4}$/';   if(!preg_match($email_exp,$email_from)) {     $error_message .= email address entered not appear valid.<br />';   }     $string_exp = "/^[a-za-z .'-]+$/";   if(!preg_match($string_exp,$first_name)) {     $error_message .= 'the first name entered not appear valid.<br />';   }   if(!preg_match($string_exp,$last_name)) {     $error_message .= 'the last name entered not appear valid.<br />';   }   if(strlen($comments) < 2) {     $error_message .= 'the comments entered not appear valid.<br />';   }   if(strlen($error_message) > 0) {     died($error_message);   }     $email_message = "zawartosc wypelnionego formularza\n\n";      function clean_string($string) {       $bad = array("content-type","bcc:","to:","cc:","href");       return str_replace($bad,"",$string);     }      $email_message .= "first name: ".clean_string($first_name)."\n";     $email_message .= "last name: ".clean_string($last_name)."\n";     $email_message .= "email: ".clean_string($email_from)."\n";     $email_message .= "telephone: ".clean_string($telephone)."\n";     $email_message .= "comments: ".clean_string($comments)."\n";   // create email headers $headers = 'from: '.$email_from."\r\n". 'reply-to: '.$email_from."\r\n" . 'x-mailer: php/' . phpversion(); if(mail($email_to, $email_subject, $email_message)) {  echo "dziekuje za kontakt postaram sie odezwac jak najszybciej | thank contacting me. in touch soon.";   echo '<input type="button" value="back" onclick="history.back();">';   }else{            echo "nie wyslano z z powodu nieznanego bledu prosze sprobuj ponownie lub zadzwon mnie bezposrednio | form not send due unknown reason please go , try again if not please try rich me directly on phone.<br/><br/><br/><br/>";       echo '<input type="button" value="back" onclick="history.back();">';   } } // die(); 

change from

<form action="send_form_email.php" method="post" class="form-horizontal"> 

to

<form action="" method="post" class="form-horizontal"> 

then save html file php , add php code in top of form page

updated php code custom style error messages.

<?php if(isset($_post['email']))  {      // change 2 lines below     $email_to = "myemail@gmail.com";      $email_subject = "nowy formularz od osoby";       function died($error)     {         // error code can go here         // adding bootstrap style class         echo '<div class="alert alert-warning" role="alert">';         echo "we sorry, there error(s) found form submitted. ";         echo "these errors appear below.<br /><br />";         echo $error."<br /><br />";         //not needed more         //echo "please go , fix these errorsbr /><br />";         //echo '<input type="button" value="back" onclick="history.back();">';         echo '</div>';         die();     }      // new function style error messages     function styleerrormsg($string)     {         $string = '<div class="alert alert-warning" role="alert">'.$string.'</div>';         return $string;     }      // validation expected data exists     if(!isset($_post['first_name']) ||         !isset($_post['last_name']) ||         !isset($_post['email']) ||         !isset($_post['telephone']) ||         !isset($_post['comments']))     {         died('we sorry, there appears problem form submitted.');            }      $first_name = $_post['first_name']; // required     $last_name = $_post['last_name']; // required     $email_from = $_post['email']; // required     $telephone = $_post['telephone']; // not required     $comments = $_post['comments']; // required      $error_message = "";      $email_exp = '/^[a-za-z0-9._%-]+@[a-za-z0-9.-]+\.[a-za-z]{2,4}$/';     if(!preg_match($email_exp,$email_from))     {         $error_message .= styleerrormsg('the email address entered not appear valid.<br />');     }      $string_exp = "/^[a-za-z .'-]+$/";     if(!preg_match($string_exp,$first_name))     {         $error_message .= styleerrormsg('the first name entered not appear valid.<br />');     }      if(!preg_match($string_exp,$last_name))     {         $error_message .= styleerrormsg('the last name entered not appear valid.<br />');     }      if(strlen($comments) < 2)     {         $error_message .= styleerrormsg('the comments entered not appear valid.<br />');     }      if(strlen($error_message) > 0)     {         died($error_message);     }     $email_message = "zawartosc wypelnionego formularza\n\n";      function clean_string($string)     {         $bad = array("content-type","bcc:","to:","cc:","href");         return str_replace($bad,"",$string);     }      $email_message .= "first name: ".clean_string($first_name)."\n";     $email_message .= "last name: ".clean_string($last_name)."\n";     $email_message .= "email: ".clean_string($email_from)."\n";     $email_message .= "telephone: ".clean_string($telephone)."\n";     $email_message .= "comments: ".clean_string($comments)."\n";   // create email headers $headers = 'from: '.$email_from."\r\n". 'reply-to: '.$email_from."\r\n" . 'x-mailer: php/' . phpversion();     if(mail($email_to, $email_subject, $email_message))     {         echo "dziekuje za kontakt postaram sie odezwac jak najszybciej | thank contacting me. in touch soon.";          echo '<input type="button" value="back" onclick="history.back();">';      }     else     {          echo "nie wyslano z z powodu nieznanego bledu prosze sprobuj ponownie lub zadzwon mnie bezposrednio | form not send due unknown reason please go , try again if not please try rich me directly on phone.<br/><br/><br/><br/>";         echo '<input type="button" value="back" onclick="history.back();">';      } } // die(); ?> 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -