javascript - ajax php post not sending email -
im unsure why isnt working when visit php script , manually send post data works when send via js gets correct response(success) email doesnt send
form:
<div name="submited" id="submitted" class="col-md-6 col-sm-6 contact_form"> <div class="con-fm"> <div class="col-sm-12"> <p class="contact-success" id="contact-success">thank you, have emailed scottish enterprise</p> <p class="contact-error">error! please contact site administrators</p> <p class="contact-error-1">error! please fill in form fields</p> <p class="contact-error-2">error! need enter name</p> <p class="contact-error-3">error! need enter email address</p> <p class="contact-error-4">error! need enter message</p> <p class="contact-error-5">error! file type should .docx, .pdf, .rtf or .doc</p> </div> <form id="hideme" method="post" enctype="multipart/form-data"> <div class="form-group"> <label for="name">name:</label> <input type="text" class="form-control" name="name" id="name"> </div> <div class="form-group"> <label for="email">email:</label> <input type="email" class="form-control" name="email" id="email"> </div> <div class="form-group"> <label for="comment">message:</label> <textarea class="form-control" rows="5" name="comment" id="comment"></textarea> </div> <div class="form-group"> <label for="att">attachment:</label> <div class="row"> <div class="col-md-5"> <div> <div><input id="upload" name="file" type="file" id="file" /></div> </div> </div> <div class="col-md-1"> </div> <div class="col-md-5 btn11"> <button value="submit" name="submit" id="submit" class="btn btn-default">send email</button> </div> </div> </div> </form> </div> </div>
the js:
<script type="text/javascript"> $(function() { $(".contact_form").submit(function() { var email = $("#name").val(); var name = $("#email").val(); var comment = $("#comment").val(); var file = $("#file").val(); var submit = $("#submit").val(); $.ajax({ url: "email.php", type: "post", data: {email: email, name: name, comment: comment, file: file, submit: submit}, datatype: "json", success: function(data) { if(data.status == 'success'){ $('#hideme').fadein().delay(0).fadeout(); $('#contact-success').fadein().delay(3000); }else if(data.status == 'error'){ $('.contact-error').fadein().delay(6000).fadeout(); }else if(data.status == 'error1'){ $('.contact-error-1').fadein().delay(6000).fadeout(); }else if(data.status == 'error2'){ $('.contact-error-2').fadein().delay(6000).fadeout(); }else if(data.status == 'error3'){ $('.contact-error-3').fadein().delay(6000).fadeout(); }else if(data.status == 'error4'){ $('.contact-error-4').fadein().delay(6000).fadeout(); }else if(data.status == 'error5'){ $('.contact-error-5').fadein().delay(6000).fadeout(); } }, error: function(msg) { $('.contact-error').fadein().delay(6000).fadeout(); } }); return false; }); }); </script>
the php:
<?php function remove_bad ($input){ $output = htmlentities(trim($input)); return $output; } function error ($input){ $output = $input; return $output; } if(isset($_post['name']) && isset($_post['email']) && isset($_post['comment']) && isset($_post['submit']) && !empty($_post['name']) && !empty($_post['email']) && !empty($_post['comment']) && !empty($_post['submit'])){ $name = remove_bad($_post['name']); $email = remove_bad($_post['email']); $comment = remove_bad($_post['comment']); if(isset($_files['file']['name']) && !empty($_files['file']['name'])){ $folder = "upload/"; $temp = explode(".", $_files["file"]["name"]); $newfilename = round(microtime(true)).'.'. end($temp); $db_path ="$folder".$newfilename ; $listtype = array( '.doc'=>'application/msword', '.docx'=>'application/vnd.openxmlformats-officedocument.wordprocessingml.document', '.rtf'=>'application/rtf', '.pdf'=>'application/pdf'); if ( is_uploaded_file( $_files['file']['tmp_name'] ) ) { if($key = array_search($_files['file']['type'],$listtype)) {if (move_uploaded_file($_files['file'] ['tmp_name'],"$folder".$newfilename)) { require_once('mail/class.phpmailer.php'); $emaila = new phpmailer(); $emaila->from = $email; $emaila->fromname = $name; $emaila->subject = 'capital'; $emaila->body = $comment; $emaila->addaddress( 'myemail@email.com' ); $file_to_attach = $folder.$newfilename; $emaila->addattachment( $file_to_attach , $folder.$newfilename ); $emaila->send(); $response_array = array(); $response_array['status'] = 'success'; header('content-type: application/json'); echo json_encode($response_array); //exit(header("location: index.php#submitted")); } }else{ $response_array = array(); $response_array['status'] = 'error5'; header('content-type: text/html'); echo json_encode($response_array); //$error = "file type should .docx, .pdf, .rtf or .doc"; } } else { $response_array = array(); $response_array['status'] = 'error5'; header('content-type: application/json'); echo json_encode($response_array); //$error = "file type should .docx or .pdf or .rtf or .doc"; } }else{ require_once('mail/class.phpmailer.php'); $emaila = new phpmailer(); $emaila->from = $email; $emaila->fromname = $name; $emaila->subject = 'capital'; $emaila->body = $comment; $emaila->addaddress( 'myemail@email.com' ); $emaila->send(); $response_array = array(); //$response_array['status'] = 'success'; $response_array['status'] = 'success'; header('content-type: application/json'); echo json_encode($response_array); } }elseif(isset($_post['name']) && isset($_post['email']) && isset($_post['comment']) && isset($_post['submit']) && !empty($_post['name']) && !empty($_post['email']) && empty($_post['comment']) && !empty($_post['submit'])){ // $error = error("you need enter message"); $response_array = array(); $response_array['status'] = 'error4'; header('content-type: application/json'); echo json_encode($response_array); }elseif(isset($_post['name']) && isset($_post['email']) && isset($_post['comment']) && isset($_post['submit']) && !empty($_post['name']) && empty($_post['email']) && !empty($_post['comment']) && !empty($_post['submit'])){ // $error = error("you need enter email address"); $response_array = array(); $response_array['status'] = 'error3'; header('content-type: application/json'); echo json_encode($response_array); }elseif(isset($_post['name']) && isset($_post['email']) && isset($_post['comment']) && isset($_post['submit']) && empty($_post['name']) && !empty($_post['email']) && !empty($_post['comment']) && !empty($_post['submit'])){ //$error = error("you need enter name"); $response_array = array(); $response_array['status'] = 'error2'; header('content-type: application/json'); echo json_encode($response_array); }elseif(empty($_post['name']) or empty($_post['email']) or empty($_post['comment']) && !empty($_post['submit'])){ // $error = error("please fill in form fields"); $response_array = array(); $response_array['status'] = 'error1'; header('content-type: text/html'); echo json_encode($response_array); } ?>
$('body').on('click', '#product_edit_custom_add_btn', function () { var mtitle = $('#custom_product_edit_name_tb').val(); var mdescription = $('#custom_product_edit_description_tb').val(); var producttitle = $('#custom_product_edit_name_tb').val(); var productprice = $('#custom_product_edit_price_tb').val(); var producturl = $('#custom_product_edit_website_tb').val(); var lefttimelimit = $('#starttime').text(); var righttimelimit = $('#endtime').text(); var file_data = $("#edit_product_photo_uploader").prop("files")[0]; console.log("filesss", file_data); var form_data = new formdata(); form_data.append("videoid", mindexvideoidg) form_data.append("setid", mindexsetidg) form_data.append("starttime", lefttimelimit) form_data.append("endtime", righttimelimit) form_data.append("mtype", 3) form_data.append("msubtype", 3) form_data.append("mtitle", mtitle) form_data.append("mdescription", mdescription) form_data.append("producttitle", producttitle) form_data.append("productprice", productprice) form_data.append("producturl", producturl) form_data.append("file", file_data) if (mtitle == "" || file_data == undefined) { $('#custom_product_edit_name_tb').css("border", "2px red solid"); $('#edit_product_photo_prev').css("border", "2px red solid"); } else { $.ajax({ cache: false, contenttype: false, processdata: false, url: '/mindexeditor/addcustomproduct', method: 'post', data: form_data, success: function (response) { if (response.status == "success") { emptyeditsectioncustomproducttb(); updateproductviewsection(); updatecustomproductmanagerview(); showtoast("success", "new person added successfully"); } } }) } });
you need send file , data in way . work.
Comments
Post a Comment