What you do depends on what you want as a response from WP. If the form’s Ajax handler is already in place and you merely want to submit with ES6 instead of jQuery, that’s fine. Be sure the form data includes an “action” value (usually as a hidden field). Ajax data is normally submitted to /wp-admin/admin-ajax.php. What you get back as a response varies greatly by what Ajax handler code is in place server side.
PHP is kind of finicky about what sort of data stream is acceptable. For jQuery .ajax(), specifying dataType : 'json', works for me. I suggest whatever the ES6 equivalent is should suffice.
Disclaimer: As you might suspect from that last line, I don’t know ES6 very well. But as long as admin-ajax.php receives properly formatted data, it doesn’t matter what language sent the request.
Thread Starter
roelof
(@roelof)
oke,
Then maybe look at the jquery how i can dynamically make a div for the errors or the success box.
I know how to do that with plain js but not with jquery
Why not use the language you’re comfortable with? WP doesn’t care what sent the data.
If you want to stick with jQuery to broaden your coding experience, when you enqueue your jQuery code, be sure to specify 'jquery' as a dependency so that the jQuery library gets loaded. The jQuery used by WP runs in “noConflict” mode, meaning the common $ shortcut will not work. Either use the full jQuery or place your code within a “noConflict” wrapper. For example:
(function( $ ) {
"use strict";
// javascript code here. i.e.: $(document).ready( function(){} );
})(jQuery);
While it’s possible to add divs for success or error, usually these already exist on the page, but they are initially empty. The success or error processes then simply populate the div content as appropriate. It’s easier to populate an existing empty div than to insert a new one. For example, the error: process of .ajax() could do:
$("#error-div").html("Sorry, that didn't work");