0

I have created a plugin that will submit a form via ajax. Below is the php function.

add_action('wp_ajax_nopriv_audience_intel', 'audience_intel_ajax');
add_action('wp_ajax_audience_intel', 'audience_intel_ajax');
function audience_intel_ajax() {
    $likeit = $_POST['likeit'];
    $response = $_POST['response'];
    $postid = $_POST['postid'];

    add_option( 'testing_form_like', $likeit );
    add_option('testing_form_response', $response);

    global $wpdb;
    $tablename = $wpdb->prefix . "intel";

    $newdata = array(
        'radio' => $likeit,
        'feedback' => $response,
        'postid' => $postid
    );    

        $wpdb->insert (
        $tablename,
        $newdata
    );

    $result = "<p>Finished successfully!</p>";
    // json_encode($result);
    echo $result;
}

Here is my script.

jQuery(document).ready(function($) {
$('.audience_intel_container .close').click(function(){
    $('.audience_intel_container').fadeOut('fast', function(){

    });
});

$('.audience_intel_container button').click(function(){
    console.log('clicked');
    var formdata = $('#audience_intel_form').serialize();
    $.ajax({
        type : "post",
        dataType : "html",
        url : audience_intel_js.ajaxurl,
        data : 'action=audience_intel&'+formdata,
        success: function(response) {
            alert(response);
            $('.audience_intel_container').html(response);
        }
    })
})   
});

The form data is added to the database correctly, but I am not able show an output message after the request is complete. I have tried both return, echo, and echo json_encode in the ajax function.

I appreciate any insight.

6
  • After echo $result; you need to exit;
    – webaware
    Commented Jan 9, 2013 at 8:30
  • I have added the exit. But, I am still not getting any results. Commented Jan 9, 2013 at 8:38
  • If you watch the network tab in Chrome's developer tools or Firefox's Firebug extension, what is coming back from the server? Is it running successfully or generating an error? What is the output it returns? And: if the output is HTML, why are you telling jQuery that it's JSON?
    – webaware
    Commented Jan 9, 2013 at 8:42
  • Use die($result); instead of echo, and try logging the response in the the success function console.log(response); , and I think you should replace dataType: "json" with "html".
    – Pierre
    Commented Jan 9, 2013 at 8:53
  • FYI, die($result); and echo $result; exit; will do the same thing.
    – webaware
    Commented Jan 9, 2013 at 8:54

2 Answers 2

2

This is the final code I am using. Thanks to webaware for pointing out that I was not using the correct datatype.

   jQuery(document).ready(function($) {
        $('.audience_intel_container .close').click(function(){
        $('.audience_intel_container').fadeOut('fast', function(){

        });
});

$('.audience_intel_container button').click(function(){
    console.log('clicked');
    var formdata = $('#audience_intel_form').serialize();
    $.ajax({
        type : "post",
        dataType : "html",
        url : audience_intel_js.ajaxurl,
        data : 'action=audience_intel&'+formdata,
        success: function(response) {
            alert(response);
            $('.audience_intel_container').html(response);
        }
    })
})   
});

php

add_action('wp_ajax_nopriv_audience_intel', 'audience_intel_ajax');
add_action('wp_ajax_audience_intel', 'audience_intel_ajax');
function audience_intel_ajax() {
$likeit = $_POST['likeit'];
$response = $_POST['response'];
$postid = $_POST['postid'];

add_option( 'testing_form_like', $likeit );
add_option('testing_form_response', $response);

global $wpdb;
$tablename = $wpdb->prefix . "intel";

$newdata = array(
    'radio' => $likeit,
    'feedback' => $response,
    'postid' => $postid
);

$wpdb->insert (
    $tablename,
    $newdata
);

$result = "<p>Finished successfully!</p>";
// json_encode($result);
echo $result;
exit;
}
0

You will be better off using the $.post for ajax. Have a look here http://codex.wordpress.org/AJAX_in_Plugins for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.