html - Unable to get form to submit with basic PHP and Bootstrap, validation working but no posting -
i'm sure has been asked many times before, being amateur not of others answers work case.
this php form i'm including on html page email removed
<?php $nameerr = $emailerr = $phoneerr = $messageerr = ""; $name = $email = $phone = $message = ""; $to = "email@gmail.com"; $from = "email@site.com"; $headers = "from: $email \r\n"; $subject = "new submission"; $body = "new loan request: \n $message"; mail(to,from,subject,headers,body) if ($_server["request_method"] == "post") { if (empty($_post["name"])) { $nameerr = "name required"; } else { $name = test_input($_post["name"]); } if (empty($_post["email"])) { $emailerr = "email required"; } else { $email = test_input($_post["email"]); } if (empty($_post["phone"])) { $phoneerr = ""; } else { $phone = test_input($_post["website"]); } if (empty($_post["message"])) { $messageerr = ""; } else { $message = test_input($_post["message"]); } } if ($_server["request_method"] == "post") { $name = test_input($_post["name"]); $email = test_input($_post["email"]); $website = test_input($_post["phone"]); $comment = test_input($_post["message"]); } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>
and html of form...the validation working i'm unable post after submission or submission email address i'm using.
<form name="sentmessage" id="contactform" method="post" action="<?php echo htmlspecialchars($_server["php_self"]);?>"> <div class="row"> <div class="col-md-6"> <div class="form-group"> <input type="text" class="form-control" placeholder="your name *" id="name" required data-validation-required-message="please enter name."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <input type="email" class="form-control" placeholder="your email *" id="email" required data-validation-required-message="please enter email address."> <p class="help-block text-danger"></p> </div> <div class="form-group"> <input type="tel" class="form-control" placeholder="your phone *" id="phone" required data-validation-required-message="please enter phone number."> <p class="help-block text-danger"></p> </div> </div> <div class="col-md-6"> <div class="form-group"> <textarea class="form-control" placeholder="your scenario *" id="message" required data-validation-required-message="please enter message."></textarea> <p class="help-block text-danger"></p> </div> </div> <div class="clearfix"></div> <div class="col-lg-12 text-center"> <div id="success"></div> <button type="submit" class="btn btn-primary btn-xl" value="submit" name="submit" id="submit">send message</button> </div> </div> </form>
seems have syntax error.
right here
mail(to,from,subject,headers,body)
should be
mail(to,from,subject,headers,body);
missing semi-colon.
i ignore none of variables mail function call correct either ... hint ... hint ... missing $
.... yup, each of syntax error too.
it may ... um ... informative turn on error reporting
<?php ini_set('display_errors', 1); error_reporting(-1);
you find validation works best if it's done before email. stands of variables
( if can use term loosely ) have in them.
and
$name = $email = $phone = $message = "";
is weirding me out bit.
update here cleaned in way it.
<?php if( isset( $_post['submit'] ) ){ $errors = []; $input = []; foreach( $_post $key => $value ){ $value = htmlspecialchars( stripslashes( trim($value) ) ); switch( $key ){ case 'submit': continue; //ignore break; //break not needed here, whatever ... // case 'email': validate email etc. // break; default: if( empty( $value ) ){ $errors[$key] = ucfirst( $key ).' required.'; }else{ $input[$key] = $value; } break; //break not needed here, whatever ... } //end switch }//end foreach $to = "email@gmail.com"; $headers = "from: {$input['email']} \r\n"; $subject = "new submission"; $body = "new loan request: \n {$message}"; //message undefined if( empty( $erros ) ){ mail($to,$input['from'],$subject,$headers,$body); }else{ //do $errors } } //end if submit //?>
i never use ending php tags unless file has html , php ( mine never ), closing tags optional anyway. leaving them in can cause un-intended output, such having line return after closing tag, can real annoying when doing file downloads.
but, that's me , how it. else may tell different.
i made example, show how better using array stuff is. think later if want add in address fields. street
, zip
, state
, county
, country
or have you.
if these in less automated fashion, code grow , grow , grow, because have add in each field. you'll adding in 6 lines of code each one, before use data. using array, add switch statement if has special requirement.
i'm lazy adding code in time ....
Comments
Post a Comment