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.
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.
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(This now creates the entry and generates no HTML output.
'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 );
Please login or Register to submit your answer