title: Using AJAX in a WordPress plugin
body: <p>I'm trying to create a WordPress sample plugin based in AJAX. I read a tutorial and did a plugin, but it's not working. I am new to AJAX. Here is the code I tried: </p>
  
  <pre><code>&lt;?php
  class ajaxtest {
  
      function ajaxcontact() {
          ?&gt;
          &lt;div id="feedback"&gt;&lt;/div&gt;
          &lt;form name="myform" id="myform"&gt;
              &lt;li&gt;
                  &lt;label for fname&gt;First Name&lt;/label&gt;&lt;input type="text" id="fname" name="fname" value=""/&gt;
              &lt;/li&gt;
              &lt;li&gt;
                  &lt;label for lname&gt;Last Name&lt;/label&gt;&lt;input type="text" id="lname" name="lname" value=""/&gt;
              &lt;/li&gt;
              &lt;input type="submit" value="Submit" id="submit" name="submit"/&gt;
          &lt;/form&gt;
          &lt;script type="text/javascript"&gt;
              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;
              }
          &lt;/script&gt;
          &lt;?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'));
  </code></pre>
  
  <p>I used this as a shortcode, but I didn't get an output. What's the mistake?</p>