WordPress.org

Ready to get started?Download WordPress

Forums

Form with javascript/ajax does not work in WordPress (4 posts)

  1. capbussat
    Member
    Posted 1 year ago #

    I use a template to hold a form. A plugin register javascript and jQuery. The form is processed by javascript/AJAX and sent to a PHP file to process its $_POST content and return it to the template page filling a <div>.

    I tried all the code before, without includin in my WordPress files works perfectly. The process php file returns HTML which is included in a <div> by ajax script.

    The same code does not work in wordpress. Firebug shows that javascript is working, the form is emptied but the result never gets back. I wonder about html() method, but it works without WordPress.
    Any suggestions?

    I include 4 files: template, plugin php, javascript file and process file.

    http://pastebin.com/ixZkD8yL

  2. capbussat
    Member
    Posted 1 year ago #

    There was an error with url, so process php file was not found. I will put some code to correct this.

    Neverteheless data is returned 0, any suggestions?

    jQuery.post("../wp-content/plugins/itm_simple_paypal_process.php",jQuery(form).serialize(), function(data){
            // place some code to find plugin directory
    	jQuery ('#destino').html(data);  // fills the div with data
    	alert(data); // data appears on alert window
    		    });
    	   });
  3. bcworkz
    Member
    Posted 1 year ago #

    I'm not sure why, but conventional AJAX seems to be problematic in WordPress, as you have discovered. I suggest you restructure your code to submit the form via admin-ajax.php, which means enqueuing your script via the 'admin_enqueue_scripts' action, even for front end related code. The server response is then implemented by hooking the action defined in part by the 'action' value passed by jQuery.post(). See AJAX in Plugins for more detailed information on this approach.

  4. capbussat
    Member
    Posted 1 year ago #

    Thanks, really it seems this the case. I have to reformulate the code to some AJAX guidelines for WordPress. I am using as guidelines this three pages:
    A simple example, that helps me to understand it ...
    http://ocaoimh.ie/2008/11/01/make-your-wordpress-plugin-talk-ajax/
    Codex itself
    http://codex.wordpress.org/AJAX_in_Plugins
    A few tips
    http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/

Topic Closed

This topic has been closed to new replies.

About this Topic