WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm attempting add some ajax to an existing script, all goes well for the first submit, but subsequent ones are not working as expected.

add_action('wp_head', 'my_special_action_javascript');
add_action('wp_ajax_my_special_action', 'my_special_action_callback');
add_action('wp_ajax_nopriv_my_special_action', 'my_special_action_callback');
add_action('wp_ajax_my_cart', 'my_cart_callback');
add_action('wp_ajax_nopriv_my_cart', 'my_cart_callback');

and prior to those I also have a wp_enqueue_script('jquery');

function my_special_action_javascript() {
<script type="text/javascript">
jQuery(document).ready(function() {
 jQuery(".addtocart").submit(function() {
  var data = {action: 'my_special_action',post:jQuery(this).serialize() };
  jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", data,
  function upCart(){
   var tdata = {action: 'my_cart'};
   jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", tdata,
  setTimeout (upCart,500);
  return false;


.addtocart is the class of the form (and there are going to be occasions when there are multiple forms on a page.)
.eshopax is a section within the form where I am displaying the response (though really I should add this via the script as well).
.ajaxcart is the cart in the widgets which I am trying to auto update.
setTimeout - added to slow things down for updating the cart, to allow the first process to finish.

Now as explained the above works as intended, once. But when I submit the form again, even with different values the display is no longer changed, yet the cart is updated (upCart) and no response text is visibly added. Additionally it doesn't appear to recognise changed form details.

So how do I reset things for it to recognise possible new form details? Additionally I don't think the jQuery.post in the upCart function is correct either (but it does work...).

any help appreciated.

(originally asked over on the WordPress support forums).

share|improve this question

ahh nvm, sorted it:

        var data = {action: 'my_special_action',post:$('.addtocart').serialize() };  
        $.post("<?php echo admin_url('admin-ajax.php'); ?>", data,
            setTimeout (clearCart,200); 
            setTimeout (doRequest,500);  
            setTimeout (clearRequest,3000);  
        function doRequest(){
            var tdata = {action: 'my_cart'};
            $.post("<?php echo admin_url('admin-ajax.php'); ?>", tdata,
        function clearRequest(){
        function clearCart(){
        return false;

share|improve this answer
@Rich Pedley: what do you mean by "sorted it"? Did you fix the problem? – Peter Mortensen Sep 14 '10 at 0:39

Your Answer


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