Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I trying to create a wordpress sample plugin with ajax base. I read out the tutorial and did plugin(is not working) Because i am new to ajax. Here is the code i tried is of form data in a ajax way.

    class ajaxtest {

        function ajaxcontact() {
            <div id="feedback"></div>
            <form name="myform" id="myform">
                    <label for fname>First Name</label><input type="text" id="fname" name="fname" value=""/>
                    <label for lname>Last Name</label><input type="text" id="lname" name="lname" value=""/>
                <input type="submit" value="Submit" id="submit" name="submit"/>
            <script type="text/javascript">

                function ajaxSubmit() {

                    var newcontact = jQuery(this).serialize();

                        type: "POST",
                        url: "/wp-admin/admin-ajax.php",
                        data: newcontact,
                        success: function(data) {

                    return false;

        function addcontact() {
            $fname = $_POST['fname'];
            if ($fname != "") {
                echo "Your Data is" . $fname;
            } else {
                echo "Data you Entered is wrong";



    function jquery_add_to_contact() {
        wp_enqueue_script('jquery');  // Enqueue jQuery that's already built into WordPress

    add_action('wp_enqueue_scripts', 'jquery_add_to_contact');
    add_action('wp_ajax_addcontact', array('ajaxtest', 'addcontact'));
add_action('wp_ajax_nopriv_addcontact', array('ajaxtest', 'addcontact')); // not really needed
    add_shortcode('cform', array('ajaxtest', 'ajaxcontact'));

I used this using shortcode. But my bad i didn't get output, i don't know what i did mistake any suggestion would be great, point out problem is much appreciated.

share|improve this question
Try with a working example: How to Use Ajax in a WordPress Shortcode? –  brasofilo Aug 20 '13 at 21:04
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.