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.

    <?php
    class ajaxtest {

        function ajaxcontact() {
            ?>
            <div id="feedback"></div>
            <form name="myform" id="myform">
                <li>
                    <label for fname>First Name</label><input type="text" id="fname" name="fname" value=""/>
                </li>
                <li>
                    <label for lname>Last Name</label><input type="text" id="lname" name="lname" value=""/>
                </li>
                <input type="submit" value="Submit" id="submit" name="submit"/>
            </form>
            <script type="text/javascript">
                jQuery('#submit').submit(ajaxSubmit);

                function ajaxSubmit() {

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

                    jQuery.ajax({
                        type: "POST",
                        url: "/wp-admin/admin-ajax.php",
                        data: newcontact,
                        success: function(data) {
                            jQuery("#feedback").html(data);
                        }
                    });

                    return false;
                }
            </script>
            <?php
        }

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

            die();
        }

    }

    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
1  
Try with a working example: How to Use Ajax in a WordPress Shortcode? –  brasofilo Aug 20 '13 at 21:04

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.