AJAX Cookie Output Issue

By: Andy Sawyer | Asked: 10/26/2022
ForumsCategory: Code HelpAJAX Cookie Output Issue
Andy Sawyer asked 1 year ago

Long story short, we are trying to create an entry and then redirect to another page using PHP.

Essentially:

FrmEntry::create( $very_cool_data );
header('Location: VERY COOL URL' );

For the PHP header() function to work, it has to be called before any output has been sent. For some reason, the FrmEntry::create() call is printing out some jQuery AJAX stuff and causing the redirect to fail. See attached image.

It seems to do this regardless of whether or not the "Submit this form with AJAX" option is set.

My question is, is there a way to suppress that AJAX markup, or is there another way to programmatically create an entry that doesn't generate output?

We're very capable of writing our own code to do this, but I'm hoping someone can clarify why calling FrmEntry::create would generate any HTML output, especially when there are no AJAX settings enabled for that particular form. 

Attachments
3 Answers
Victor Font answered 1 year ago
This isn't making sense. FrmEntry::create( $values ) doesn't use Ajax. It's a PHP function that runs on the server. There's no AJAX involved unless you're triggering Ajax from the front end. Your screen capture of your Ajax just appears to be setting a cookie. There's no redirect being set. Regardless of what you are trying to do, the jQuery you are using to set a cookie via Ajax will always return an Ajax response. FrmEntry::create( $values ) returns an id for the newly created entry, which you are not capturing on a variable like this: $newid = FrmEntry::create( $values ). There's not enough information about your process provided to understand what's going wrong. Where is the FrmEntry::create( $values ) complete code running? What filter or function are you using.
Andy Sawyer answered 1 year ago

Thanks for taking a look @Victor Font! The AJAX script in the screenshot is being generated by Formidable when I call FrmEntry::create().  You can see their code in formidable-pro/classes/views/frmpro-entries/set_cookie.php:

<?php
if ( ! defined( 'ABSPATH' ) ) {
	die( 'You are not allowed to call this page directly.' );
}
?>
<script type="text/javascript">
jQuery(document).ready(function($){
jQuery.ajax({type:'POST',url:'<?php echo esc_url_raw( admin_url( 'admin-ajax.php' ) ); ?>',
data:"action=frm_entries_ajax_set_cookie&entry_id=<?php echo (int) $entry_id; ?>&form_id=<?php echo (int) $form_id; ?>"
});
});
</script>

  In this example:

FrmEntry::create( $very_cool_data );
header(\'Location: VERY COOL URL\' );
echo \"did not redirect...\";

The header call silently fails and the output to the page is what\'s in my screenshot. Again, the screenshot is what is generated by Formidable when FrmEntry::create is called, not something we wrote.

Really, all I need to do it manually create an entry at the top of a template file and then redirect to another page. We\'ve done this in the past without issue, so I was hoping someone could help explain why FrmEntry::create() generates HTML output.

Andy Sawyer answered 1 year ago
If anyone else finds themself here, I believe I have figured out a solution. Digging through the code base, there is a check for a POST parameter called frm_skip_cookie. See below. 
public static function maybe_set_cookie( $entry_id, $form_id ) {
        if ( defined('WP_IMPORTING') || defined('DOING_AJAX') || defined('REST_REQUEST') ) {
            return;
        }

        if ( isset($_POST) && isset($_POST['frm_skip_cookie']) ) {
            self::set_cookie($entry_id, $form_id);
            return;
        }

        include(FrmProAppHelper::plugin_path() . '/classes/views/frmpro-entries/set_cookie.php');
    }
In my code, I added this to the data being sent to FrmEntry::create():
            $entry = array(
'form_id' => $form_id, 'item_key' => $item_key
'frm_skip_cookie' => true, 'item_meta' => $data ); $_POST = $entry; $entry_id = FrmEntry::create( $_POST ); header('Location: '. $redirect_url );
This now creates the entry and generates no HTML output.

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right