Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing AJAX in a WordPress plugin
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload