php - Javascript Ajax - Contact form does not display prompts and send button doesn't work -
i looking on different examples contact form sent using javascript / ajax. solutions don't seems work. i'm trying rewrite existing chunk of code. here left (with few of own modifications).
i don't quite understand functionality going wrong. send button nothing, not send email , goes top of page.
javascript:
<script type="text/javascript"> $('#submit').click(function(){ var name = $("#name").val(); var email = $("#email").val(); var phone = $("#phone").val(); var message = $("#message").val(); if (name == "") { $('.errormess').html('<div class="alert alert-warning"><strong>please insert name</strong></div>'); return false; } if (email == "") { $('.errormess').html('<div class="alert alert-warning"><strong>please insert email</strong></div>'); return false; } if (phone == "") { $('.errormess').html('<div class="alert alert-warning"><strong>please insert phone number</strong></div>'); return false; } if (message == "") { $('.errormess').html('<div class="alert alert-warning"><strong>please insert text</strong></div>'); return false; } $.ajax({ type: "post", url: 'contact-us.php', data: 'name='+$("#name").val() + 'email='+$("#email").val() + 'phone=' + $("#phone").val() + 'message=' + $("#message").val(), datatype: "html", success: function(data) { if (data == 0) { $('.errormess').html('<div class="alert alert-success"><strong>your query submited successfully</strong></div>'); } else { $('.errormess').html('<div class="alert alert-danger"><strong>failed send email</strong></div>'); } } }); return true; });
this contact-us.php:
<?php $name=$_post['name']; $email=$_post['email']; $phone=$_post['phone']; $message=$_post['message']; $to = "help@belladonnait.com"; $subject = "email user"; $message = " <html> <head> <title>email user</title> </head> <body> <p>this email contains user requirement</p> <table> <tr> <th>name</th> <th>email</th> <th>phone</th> <th>message</th> </tr> <tr> <td>".$name."</td> <td>".$email."</td> <td>".$phone."</td> <td>".$message."</td> </tr> </table> </body> </html> "; $headers = "mime-version: 1.0" . "\r\n"; $headers .= "content-type:text/html;charset=utf-8" . "\r\n"; $headers .= 'from: <webmaster@example.com>' . "\r\n"; $headers .= 'cc: myboss@example.com' . "\r\n"; $ok=mail($to,$subject,$message,$headers); if ($ok) { echo "1"; } else{ echo "0"; } ?>
i think the problem data in ajax. either send in object {name:$("#name").val(),email:$("#name").val(),...} or add "&" in-front of each key ( '&name='+$("#name").val() + '&email='+$("#email").val() ). hope helps
Comments
Post a Comment