I'm building a custom jQuery form and I need to specify the siteurl in the url that loads /wp-admin/admin-ajax.php.
I place this code in the template that has the form:
<script type="text/javascript">
jQuery('#newFeedbackForm').submit(ajaxSubmit);
function ajaxSubmit(){
var newFeedbackForm = jQuery(this).serialize();
jQuery.ajax({
type:"POST",
url: <?php get_bloginfo('siteurl'); ?> . "/wp-admin/admin-ajax.php",
data: newFeedbackForm,
success:function(data){
jQuery("#feedback").html(data);
}
});
return false;
}
</script>
When I submit the form I get a 404 error with all the form inputs in the URL. I'm thinking ajax isn't being called right to handle the form and apply the function of the hidden value:
<input type="hidden" name="action" value="addComment"/>
Working backwards, here is the addComment function:
function addComment(){
global $wpdb;
$topic = $_POST['topic'];
$name = $_POST['name'];
$email = $_POST['email'];
$no_results_comment = $_POST['no_results_comment'];
// EXTRA
$date = time(); // UNIX TIMESTAMP
if($wpdb->insert('wp_no_results_feedback',array(
'topic' => $topic,
'name' => $name,
'email' => $email,
'no_results_comment' => $no_results_comment,
'date' => $date
))===FALSE){
echo "Error";
}else{
echo "Thanks for your feedback. We will get back to you shortly.";
}
die();
}
add_action('wp_ajax_addComment','addComment');
add_action('wp_ajax_nopriv_addComment','addComment');