I am trying to fill out a form in one page and open the array in a second form. I have tried a number of different methods; this one gets me the closest. However, the data doesn’t seem to be updating if I go back and resubmit the form, I get directed to page 196 and the old data is what is visible. I am at a loss as to why this is happening. The entry and output pages are both using shortcodes.
Any help or ideas would be greatly appreciated.
​
// Generate the form shortcode
function generate_form_shortcode() {
global $wpdb;
ob_start();
?>
<form method=”post” action=”<?php echo esc_url(admin_url(‘admin-post.php’)); ?>”>
<label for=”name”>Name:</label>
<input type=”text” id=”name” name=”name” required>
<br>
<input type=”submit” name=”submit_form” value=”Submit”>
<input type=”hidden” name=”action” value=”submit_name”>
</form>
<?php
$form = ob_get_clean();
return $form;
}
add_shortcode(‘entry_form’, ‘generate_form_shortcode’);
// Handle the form submission
function submit_name_handler() {
if (isset($_POST[‘submit_form’])) {
// Start the session if it hasn’t been started yet
if (!session_id()) {
session_start();
}
// Sanitize and store the submitted form data in a session variable
$_SESSION[‘form_data’] = $_POST;
wp_safe_redirect(get_permalink(196));
//exit;
}
}
add_action(‘admin_post_submit_name’, ‘submit_name_handler’);
add_action(‘admin_post_nopriv_submit_name’, ‘submit_name_handler’);
// Register the shortcode to display the submitted form data
add_shortcode(‘display_submitted_data’, ‘display_submitted_data_shortcode’);
// Create the shortcode callback function to display the submitted form data
function display_submitted_data_shortcode() {
$output = ”;
if (!session_id()) {
session_start();
}
if (isset($_SESSION[‘form_data’]) && !empty($_SESSION[‘form_data’])) {
$data = $_SESSION[‘form_data’];
foreach ($data as $key => $value) {
if ($key !== ‘submit_form’) {
$output .= ‘<li><strong>’ . sanitize_text_field($key) . ‘:</strong> ‘ . sanitize_text_field($value) . ‘</li>’;
}
}
$output .= ‘</ul>’;
// Clear the session variable after displaying the data
unset($_SESSION[‘form_data’]);
}
return $output;
}