Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AJAX functionality examples for Advanced Forms Pro version 1.8.0
jQuery(function ($) {
// Changing the response type to a redirect on the fly.
acf.addAction('af/form/ajax/submission', function (data, form) {
if (form.key === 'form_5f8f987654e0') {
data.type = 'redirect';
data.redirect_url = 'https://example.com';
}
});
});
jQuery(function ($) {
// Changing the success message.
acf.addAction('af/form/ajax/submission', function (data, form) {
if (form.key === 'form_5f8f987654e0') {
data.success_message = '<div style="background:green; color:white;">This is a new message!</div>'
}
});
});
<?php
advanced_form( 'form_5f8f987654e0', [ 'ajax' => true ]);
<?php
add_filter( 'af/form/ajax/response', function ( $response, $form, $args ) {
if ( $form['key'] === 'form_5f8f987654e0' ) {
// If our 'star_rating' ACF field has a value less than 3, redirect
// the user to another page.
if ( af_get_field( 'star_rating' ) < 3 ) {
$response['type'] = 'redirect';
$response['redirect_url'] = 'https://example.com';
}
}
return $response;
}, 10, 3 );
jQuery(function ($) {
// Triggering any custom JavaScript.
acf.addAction('af/form/ajax/submission', function (data, form) {
if (form.key === 'form_5f8f987654e0') {
// …add custom handler code here…
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment