I’m making a WordPress theme where every post will have a contact form. The contact form will collect information about the visitor and send a mail to the site administrator. It will also send an email to the visitor with a link to a downloadable PDF (this code is not done yet).
The problem I have is that the php function that handles the ajax form doesn’t respond. It only returns a -1 (No Properties). But I get a status code 200 OK.
I’m running the dev server on MAMP Pro OSX and DynDNS.
This is the PHP I have in functions.php
function ajax_contact() {
if(!empty($_POST)) {
$name = $_POST['name'];
$company = $_POST['company'];
$mail = $_POST['mail'];
$admin_mail = get_bloginfo('admin_email');
$error = "";
if(!$name) {
$error .= "Please tell us your name<br/>";
}
if(!$company) {
$error .= "Please tell us your company<br/>";
}
if(!$mail) {
$error .= "Please tell us your E-Mail address<br/>";
}
if(empty($error)) {
$subject = "New download notification";
$message = "You've received a new download notification. \n\n
Name: ".$name."\n
Company: ".$company."\n
Mail: ".$mail."\n";
// Send a mail to the admin with the contact info.
$send_mail = wp_mail($admin_mail, $subject, $message);
// Send mail to visitor with the download link.
if($send_mail) {
echo "sent";
die();
}
} else {
echo "error: " . $error;
die();
}
}
}
add_action('wp_ajax_nopriv_ajax_contact', 'ajax_contact');
add_action('wp_ajax_ajax_contact','ajax_contact');
This is the javascript code
function sendForm(formId, postId, str)
{
console.log("formId: " + formId);
console.log("formId: " + postId);
console.log("str: " + str);
$.ajax({
type: "POST",
url: "../wp-admin/admin-ajax.php",
action: "ajax_contact",
data: str,
success: function(data) {
$("#" + formId).ajaxComplete(function(event, request, settings){
if(data == 'sent') {
$("#" + formId).find(".success").fadeIn("slow");
}
else {
result = data;
$("#" + formId).siblings(".success").html(result);
}
});
}
});
}
And this is the actual form (I have multiple forms on the same page)
<form name="form-<?php the_ID(); ?>" id="form-<?php the_ID(); ?>"/>
Name <br/>
<input type="text" name="name" value=""/><br />
Company / Organisation <br/>
<input type="text" name="company" value=""/><br />
Email <br/>
<input type="text" name="mail" value=""/><br />
<input type="hidden" name="postId" value="<?php the_ID(); ?>">
<input type="submit" value="submit" class="requestbutton" id="requestButton-<?php the_ID(); ?>" rel="<?php the_ID(); ?>"/>
</form>
EDIT: Changed the ajax action name, problem still persists
Your AJAX
action:should becontact_formnotajax_contact