简体   繁体   中英

My contact form doesn't work

I have this contact form. When i press submit button it just reload the hole web page and doesn't send anything. It even doesn't show the success or error message. Please help me with that. There are my codes:

 $(document).ready(function(){ $('#contactform').on('submit',function(e) { $.ajax({ url:'action.php', data:$(this).serialize(), type:'POST', success:function(data){ console.log(data); $(".success").show().fadeOut(5000); }, error:function(data){ $(".error").show().fadeOut(5000); } }); e.preventDefault(); }); }); **<!-- This is php code: -->** <?php $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $message = $_POST['message']; $to = 'kissa101295@gmail.com'; $subject = 'Contact Form'; $msg = "First Name: $firstname\\nLast Name: $lastname\\nEmail: $email\\message: $message"; mail($to, $firstname, $msg); ?>
 <form class="contactform" action="action.php" method="POST" name="contactform" id="contactform"> <div class="errorwrap"> <div class="error">Error!</div> <div class="success">Submitted!</div> </div> <div class="input-fields"> <label>First Name: </label> <input class="text" type="text" name="firstname" id="firstname" required /> </div> <div class="input-fields"> <label>Last Name: </label> <input class="text" type="text" name="lastname" id="lastname" required /> </div> <div class="input-fields"> <label>Email: </label> <input class="text" type="text" name="email" id="email" required /> </div> <div class="input-fields"> <label> Message:</label> <textarea name="message" placeholder="Write me a message here..."></textarea> </div> <div class="buttons"> <input type="submit" value="Submit" /> </div> </form>

I think your problem is with header value in the function:

Try to add this header with this header parameters:

$headers = 'From: example@example.com' . "\r\n" .
    'Reply-To: example@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $firstname, $message, $headers);

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM