[cfdb-save-form-post] Ajax Save
-
Hi,
I have custom ajax form to capture the name and email before the user download the file. Found this [cfdb-save-form-post] it seems not working on ajax form submission.Can someone please help me.
I have remove [cfdb-save-form-post] codes since its not working on my end.
Here’s my code:
form:
<form class="form-inline" id="ajax-contact" method="post" action="<?php echo get_template_directory_uri() ?>/mailer.php"> <input type="hidden" name="form_title" value="Neighboorhood Inquiries"/> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> <h4 class="modal-title" id="myModalLabel">Download Your Neighborhood Market Report</h4> </div> <div class="modal-body"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p> <div id="form-messages"></div> <div id="form-fields"> <div class="form-group"> <input type="text" id="name" name="name" class="form-control" placeholder="Name" required> </div> <div class="form-group"> <input type="email" id="email" name="email" class="form-control" placeholder="Email Address" required> </div> <input type="hidden" name="attachment" id="attachment" value="<?php echo $report_pdf['url']; ?>"> <input type="hidden" name="page_title" id="page_title" value="<?php the_title(); ?>"> </div> </div> <div class="modal-footer"> <!-- <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> --> <button type="submit" class="btn btn-primary">Download Now</button> </div> </form>
Script:
<script type="text/javascript"> $(function() { // Get the form. var form = $('#ajax-contact'); // Get the messages div. var formMessages = $('#form-messages'); // Set up an event listener for the contact form. $(form).submit(function(e) { // Stop the browser from submitting the form. e.preventDefault(); // Serialize the form data. var formData = $(form).serialize(); // Submit the form using AJAX. $.ajax({ type: 'POST', url: $(form).attr('action'), data: formData }) .done(function(response) { // Make sure that the formMessages div has the 'success' class. $(formMessages).removeClass('error'); $(formMessages).addClass('success'); // Set the message text. $(formMessages).html(response); // console.log(response); // Clear the form. $('#name').val(''); $('#email').val(''); //$('#form-fields').hide(); $('#attachment_download ').get(0).click(); //$('#message').val(''); }) .fail(function(data) { // Make sure that the formMessages div has the 'error' class. $(formMessages).removeClass('success'); $(formMessages).addClass('error'); // Set the message text. if (data.responseText !== '') { $(formMessages).text(data.responseText); } else { $(formMessages).text('Oops! An error occured and your message could not be sent.'); } }); }); }); </script>
Mailer:
if ($_SERVER["REQUEST_METHOD"] == "POST") { // Get the form fields and remove whitespace. $name = strip_tags(trim($_POST["name"])); $name = str_replace(array("\r","\n"),array(" "," "),$name); $email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL); $attachment = trim($_POST["attachment"]); $neighborhood = trim($_POST["page_title"]); $fileatt = $attachment; // Check that data was sent to the mailer. if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) { // Set a 400 (bad request) response code and exit. http_response_code(400); echo "Oops! There was a problem with your submission. Please complete the form and try again."; exit; } // Set the recipient email address. // FIXME: Update this to your desired email address. $recipient = $email; // Set the email subject. $subject = "Here's your copy: $neighborhood Market Report"; $replyto=""; // Build the email content. $email_content = "Hi $name,<br/>"; $email_content .= "Attached file is <strong>$neighborhood</strong> Market Report."; //$email_content .= "Email: $email\n\n"; //$email_content .= "Message:\n$attachment\n"; // Build the email headers. $ext = strrchr( $fileatt , '.'); $ftype = ""; if ($ext == ".doc") $ftype = "application/msword"; if ($ext == ".jpg") $ftype = "image/jpeg"; if ($ext == ".gif") $ftype = "image/gif"; if ($ext == ".zip") $ftype = "application/zip"; if ($ext == ".pdf") $ftype = "application/pdf"; if ($ftype=="") $ftype = "application/octet-stream"; // read file into $data var $file = fopen($fileatt, "rb"); $data = fread($file, filesize( $fileatt ) ); fclose($file); // split the file into chunks for attaching $content = chunk_split(base64_encode($data)); $uid = md5(uniqid(time())); // build the headers for attachment and html $h = "From: $name <[email protected]>\r\n"; if ($replyto) $h .= "Reply-To: ".$replyto."\r\n"; $h .= "MIME-Version: 1.0\r\n"; $h .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"; $h .= "This is a multi-part message in MIME format.\r\n"; $h .= "--".$uid."\r\n"; $h .= "Content-type:text/html; charset=iso-8859-1\r\n"; $h .= "Content-Transfer-Encoding: 7bit\r\n\r\n"; $h .= $email_content."\r\n\r\n"; $h .= "--".$uid."\r\n"; $h .= "Content-Type: ".$ftype."; name=\"".basename($fileatt)."\"\r\n"; $h .= "Content-Transfer-Encoding: base64\r\n"; $h .= "Content-Disposition: attachment; filename=\"".basename($fileatt)."\"\r\n\r\n"; $h .= $content."\r\n\r\n"; $h .= "--".$uid."--"; // $email_headers = "From: $name <[email protected]>"; // Send the email. if (mail($recipient, $subject, $email_content, str_replace("\r\n","\n",$h) )) { // Set a 200 (okay) response code. http_response_code(200); echo '<p>Thank you. Your download will start automatically.</p> If not please click <a id="attachment_download" href="'.$attachment.'" download>here</a>.'; } else { // Set a 500 (internal server error) response code. http_response_code(500); echo "Oops! Something went wrong and we couldn't send your message."; } } else { // Not a POST request, set a 403 (forbidden) response code. http_response_code(403); echo "There was a problem with your submission, please try again."; }
https://www.ads-software.com/plugins/contact-form-7-to-database-extension/
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘[cfdb-save-form-post] Ajax Save’ is closed to new replies.