Topic: Script successfully calls to create record, but re-displays form after

This script below is called by the following:

    new_links.php?link_id=KAD&batch_ref=2019-01-04%20PEYSON&trans_id=1901002

It displays correctly when called.

When the "Add new" is clicked it successfully calls the db function and creates a new record.
It also correctly shows the "display_notification" which follows.
It does not display the link that follows, but goes on to re-display the form and the"Transaction 1901002 will be added..." dialog.

If the "submit" is supposed to re-call the script, then the vars that initiate the form display should have been cleared.

Seems like I am missing something about the operation of the "submit" processing.

Any help appreciated.


<?php
/*===================================================================\
|                         FrontBatchProcessing                       |
|--------------------------------------------------------------------|
|  Copyright (C) 2019 NEH Systems                                    |
|  Date:  07-March-2019                                              |
|  Description: FrontAccounting Transaction Batch Control Module     |
|  Released under GNU GPL License.                                   |
|                                                                    |
\===================================================================*/

$page_security = 'SA_NEWLNKS';
$path_to_root  = '../../..';

$module_root = $path_to_root . "/modules/BatchControl";

include_once($path_to_root . "/includes/session.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/date_functions.inc");
include_once($module_root  . "/includes/batches_db.inc");

add_access_extensions();

$js = "";
if ($SysPrefs->use_popup_windows)
    $js .= get_js_open_window(720, 640);
if (user_use_date_picker())
    $js .= get_js_date_picker();

page(_($help_context = "Batch Control"));

simple_page_mode(true);

$link_id   = '';
$batch_ref = '';
$trans_id  = '';
$target    = '';
$link      = '';

//-----------------------------------------------------------------------------

function can_process()
{
    global $selected_id;

    if (!isset($_POST['link_id']) || !isset($_POST['batch_ref']) 
                                    || !isset($_POST['trans_id']))
    {
        display_error(_("Your POST variables are not set!"), $center=true);
        return false;
    }

    if (isset($_POST['link_id']) && strlen($_POST['link_id']) < 3)
    {
        display_error(_("Invalid Link ID."));
        return false;
    }
    elseif (isset($_POST['batch_ref']) && strlen($_POST['batch_ref']) < 15)
    {
        display_error(_("Incomplete Batch Reference."));
        return false;
    }
    elseif (isset($_POST['trans_id']) && strlen($_POST['trans_id']) < 7)
    {
        display_error(_("Invalid Trans ID."));
        return false;
    }
    return true;
}

//-----------------------------------------------------------------------------

if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM')
{
    $_POST['link_id']   = $_SESSION['link_id'];    // recover post vars
    $_POST['batch_ref'] = $_SESSION['batch_ref'];
    $_POST['trans_id']  = $_SESSION['trans_id'];

    $_SESSION['link_id']   = NULL;            // clear session vars
    $_SESSION['batch_ref'] = NULL;
    $_SESSION['trans_id']  = NULL;


    if ($Mode=='ADD_ITEM' && can_process())
    {
        // function in BatchControl/includes
        create_new_link($_POST['link_id'], $_POST['batch_ref'], $_POST['trans_id']);

        display_notification(_('New transaction link ' . $_POST['trans_id'] . 
                                   ' added to selected batch ' . $_POST['link_id']));

        $link_id   = $_POST['link_id'];
        $batch_ref = $_POST['batch_ref'];

        $target = "<a href='view_links.php?link_id=$link&batch_ref=$batch_ref>";
        $link   = "View Links";

        $_GET['trans_id'] = NULL;            // kill so no tab after add

        $_POST['link_id']   = NULL;            // kill so no tab after add
        $_POST['batch_ref'] = NULL;            // after new link created
        $_POST['trans_id']  = NULL;

        echo "<center><td>";
        echo $target . $link . "</a>\n";
        echo "</center></td>\n";

        $Mode = 'RESET';
    }
    else
    {
        // will decide after consult whether editing and/or deletion allowed
    }
}

//-----------------------------------------------------------------------------
// detect if new_links was called from batch_control with trans_id.
// after adding a new transaction link this should not execute again.
//

if (isset($_GET['trans_id']) && !empty($_GET['trans_id']))
{
    $trans_id = $_GET['trans_id'];

    echo "Transaction ID: " . $trans_id . "<br />";    // testing on reload

    if (isset($_GET['batch_ref']) && !empty($_GET['batch_ref']))
        $batch_ref = $_GET['batch_ref'];

    if (isset($_GET['link_id']) && !empty($_GET['link_id']))
        $link_id = $_GET['link_id'];

    $_POST['link_id']   = $link_id;
    $_POST['batch_ref'] = $batch_ref;
    $_POST['trans_id']  = $trans_id;
}

//-----------------------------------------------------------------------------
// displays the table ONLY if new_links has been called from batch_control
// with a trans_id to be added to the batch_links table.
// after adding a new link this script should call view_links.php so that
// the updated batch_link entries can be displayed

if (isset($_POST['trans_id']) && !empty($_POST['trans_id']))
{
    start_form($multi=true);

    start_table(TABLESTYLE2);
 
    $th = array(_("Batch ID"), _("Reference"), _("Transaction ID"), "", "");

    inactive_control_column($th);
    table_header($th);

    start_row();
    echo "<td>" . $_POST['link_id'] . "</td><td>" . $_POST['batch_ref'] .
           "</td><td>" . $_POST['trans_id'] . "</td><td></td><td></td>";
    end_row();
    br(1);

    end_table(1);

    $msg = "Transaction " . $_POST['trans_id'] . " will be added to Batch " . 
                              $_POST['link_id'] .  " " . $_POST['batch_ref'];
    display_note($msg, 1, 1, "");

    $_SESSION['link_id']   = $_POST['link_id'];    // keep data in session
    $_SESSION['batch_ref'] = $_POST['batch_ref'];    // must unset after add
    $_SESSION['trans_id']  = $_POST['trans_id'];

    submit_add_or_update_center($selected_id == -1, '', 'both');

    end_form();

    end_page();
}