Note that there are some explanatory texts on larger screens.

plurals
  1. POajax form using information from current post - wordpress
    primarykey
    data
    text
    <p>I'm following this <a href="http://byronyasgur.wordpress.com/2011/06/27/frontend-forward-facing-ajax-in-wordpress/" rel="nofollow">tutorial</a> to use front-end ajax to submit an email from a form and include information such as the current post title in the email. Please note I'm not an expert of php or ajax. Any advice or resources would be appreciated.</p> <p>I tried the following - but my hidden input field value below only returns a string, I assume this is because it gets included after the page loads.</p> <p>Jquery</p> <pre><code>function submit_me(){ jQuery.post(the_ajax_script.ajaxurl, jQuery("#theForm").serialize() , function(response_from_the_action_function){ jQuery("#response_area").html(response_from_the_action_function); } ); } </code></pre> <p>PHP</p> <pre><code>// enqueue and localise scripts function plugin_enqueue_scripts() { wp_enqueue_script( 'my-ajax-handle', plugin_dir_url( __FILE__ ) . 'ajax.js', array( 'jquery' ) ); wp_localize_script( 'my-ajax-handle', 'the_ajax_script', array( 'ajaxurl' =&gt; admin_url( 'admin-ajax.php' ) ) ); } add_action('wp_enqueue_scripts', 'plugin_enqueue_scripts'); // THE AJAX ADD ACTIONS add_action( 'wp_ajax_the_ajax_hook', 'the_action_function' ); add_action( 'wp_ajax_nopriv_the_ajax_hook', 'the_action_function' ); // need this to serve non logged in users // THE FUNCTION function the_action_function(){ /* this area is very simple but being serverside it affords the possibility of retreiving data from the server and passing it back to the javascript function */ $name = $_POST['name']; $title = $_POST['title']; echo"Title: " . $title;// this is passed back to the javascript function die();// wordpress may print out a spurious zero without this - can be particularly bad if using json } // ADD EG A FORM TO THE PAGE function hello_world_ajax_frontend(){ $the_form = ' &lt;form id="theForm"&gt; &lt;input id="name" name="name" value = "name" type="text" /&gt; &lt;input name="action" type="hidden" value="the_ajax_hook" /&gt;&amp;nbsp; &lt;!-- this puts the action the_ajax_hook into the serialized form --&gt; &lt;input id="submit_button" value = "Click This" type="button" onClick="submit_me();" /&gt; &lt;input name="title" type="hidden" value="&lt;?php the_title(); ?&gt;" /&gt; &lt;/form&gt; &lt;div id="response_area"&gt; This is where we\'ll get the response &lt;/div&gt;'; return $the_form; } add_shortcode("hw_ajax_frontend", "hello_world_ajax_frontend"); ?&gt; </code></pre>
    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.
    1. This table or related slice is empty.
    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