javascript - PHP contact form will just refresh the page after submission. -
after searching 3 hours still can't figure 1 out. have html template contact form , im trying make work using php script. changed template php , pasted php form script in it. working fine except confirmation text. after successful submission refresh page instead of printing "your mail has been sent successfuly ! thank feedback". not want redirect, want print on same page.
any ideas? got sample of code.
<form action="<? echo $_server['php_self']; ?>" id="contact-form" method="post" class="form afsana-form" role="form"> <div class="row"> <div class="col-sm-12 form-group"> <input class="form-control afsana-style" id="name" name="name" placeholder="name" type="text" required autofocus /> </div> <div class="col-sm-12 form-group"> <input class="form-control afsana-style" id="email" name="email" placeholder="email" type="email" required /> </div> <div class="col-sm-12 form-group"> <textarea class="form-control" id="message" name="message" placeholder="message" rows="5"></textarea> </div> <div class="col-sm-12 form-group"> <button class="btn btn-primary afsana-btn" name="submit" value="verzenden" type="submit">verzenden <i class="ion-arrow-graph-up-right"></i></button> </div> </div> </form> <?php if(isset($_post["submit"])){ // checking blank fields.. if($_post["name"]==""||$_post["email"]==""||$_post["message"]==""){ echo "fill fields.."; }else{ // check if "sender's email" input field filled out $email=$_post['email']; // sanitize e-mail address $email =filter_var($email, filter_sanitize_email); // validate e-mail address $email= filter_var($email, filter_validate_email); if (!$email){ echo "invalid sender's email"; } else{ $subject = (contact_form); $message = $_post['message']; $headers = 'from:'. $email . "\r\n"; // sender's email $headers .= 'cc:'. $email2 . "\r\n"; // carbon copy sender // message lines should not exceed 70 characters (php rule), wrap $message = wordwrap($message, 70); // send mail php mail function mail("something@domain.com", $subject, $message, $headers); echo "your mail has been sent successfuly ! thank feedback"; } } } ?>
first, have this: $subject = (contact_form); should throw error, assume have error reporting turned off. when developing, should have error reporting on can see errors in code... else working blind. don't mean throwing tacky error_reporting(0) in every file either, mean set error reporting level e_all in php.ini.
you have: $headers .= 'cc:'. $email2 . "\r\n"; however, $email2 not defined anywhere, error here too.. why it's important test error reporting on.
see if works:
<?php $error = ''; if(isset($_post['submit'])) { if ( !empty($_post['name']) && !empty($_post['email']) && !empty($_post['message']) ) { $email = $_post['email']; $email = filter_var($email, filter_sanitize_email); if ( $email = filter_var($email, filter_validate_email) ) { $subject = '(contact_form)'; $message = $_post['message']; $headers = 'from:'. $email . "\r\n"; // sender's email $message = wordwrap($message, 70); if ( $result = mail("something@domain.com", $subject, $message, $headers) ) { $error = 'success'; } else { $error = 'there error sending email!'; } } else { $error = 'invalid email address!'; } } else { $error = 'please fill fields.'; } } ?> <p><?= $error ?></p> <form action="" method="post"> <input type="text" name="name" value="" /><br /> <input type="email" name="email" value="" /><br /> <textarea name="message" rows="5"></textarea><br /> <input type="submit" name="submit" value="submit" /> </form>
Comments
Post a Comment