Topic: Variables display but empty values sent to database

When the script below is called with a transaction id (trans_id) as well as a link_id and a batch_ref, everything displays as it should.  Both top and bottom tables.

When "Add new" is clicked, the database table 0_batch_link should have the new link added.

Even though the new link variables display correctly in the bottom table, the added row in the database contains no data other than the auto-incremented record id.

I need a fresh pair of eyes to see what is missing.  Any assistance much appreciated.

neholtz

NOTE: I have tried with both the local variables and the $_POST variables with the same result.

<?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_VIEWLNKS';
$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  = '';


//-----------------------------------------------------------------------------
// code to display items in batch_links table when called from batch_control
// the top table displays for both viewing links and also for adding new links
// if no items in the batch_links table, then display appropriate message

if (isset($_GET['link_id']) && !empty($_GET['link_id']))    // from batch_control
{
    $link_id = $_GET['link_id'];

    $result = get_links($link_id);    // function in BatchControl/includes

    if (!$result)
    {
        $msg = "No Linked Transactions Found.";    // new batch without links

        display_note($msg, 2, 2, "");

        hyperlink_back(1);
    }
    else
    {
        start_table(TABLESTYLE);

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

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

        $i = 0;
        $k = 0;

        while($row = db_fetch($result))
        {
            alt_table_row_color($k);

            $i++;

            label_cell($row['link_id']);
            label_cell($row['batch_ref']);
            label_cell($row['trans_id']);

            edit_button_cell("Edit".$row["link_id"], _("Edit"));
            delete_button_cell("Delete".$row["link_id"], _("Delete"));

            end_row();
        }

        $num = count($row)-1;

        end_table(1);

        $msg = $i . " Linked Transactions Found.";

        display_note($msg, $br=1, $br2=1, $extra="");

        hyperlink_back(1);
    }
}

//-----------------------------------------------------------------------------
// displays the bottom table if view_links has been called from batch_control
// with a trans_id to be added to the batch_links table - new link is displayed
//

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

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

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

    start_form();

    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, "");

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

    end_form();

    end_page();
}

//-----------------------------------------------------------------------------
// functions
//-----------------------------------------------------------------------------

function can_process()
{
    global $selected_id;

    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')
{
    if ($Mode=='ADD_ITEM' && can_process())
    {
        // function in BatchControl/includes
        create_new_link($link_id, $batch_ref, $trans_id);

        display_notification(_('New transaction link added to selected batch'));        
        $Mode = 'RESET';
    }
    else
    {
        // will decide after consult whether editing and/or deletion allowed
    }
}

Re: Variables display but empty values sent to database

Must you have to urlencode the link?

Re: Variables display but empty values sent to database

Tried with: start_form($multi=true);

Which generated:

<form enctype='multipart/form-data' method='post' action='/devfa/modules/BatchControl/manage/view_links.php' >

Same result:

New record written with no data other than auto-increment record id.

If the form action causes the view_links.php to be recalled so that:
1. The $Mode==ADD_ITEM can be sensed
2. Which in turn calls can_process() which throws NO errors using the same $_POST vars
3. How is it possible that those $_POST vars are empty when the create_new_link function is called?
4. Plus the fact that after "Add new" was clicked the display still shows the same data as before (same html code also).

Will try to debug using PhpStorm and update this topic.

Re: Variables display but empty values sent to database

Check scope and visibility of the variables concerned at the place where they should be available. Also check the case sensitivity of the parameters passed in for comparison.

Re: Variables display but empty values sent to database

apmuthu, you were correct, the $_POST variables were not set at that point.

I inserted the following code in the can_process() function and it produced the error message:

    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;
    }

I guess I need to understand what takes place when the "submit_add_or_update_center($selected_id == -1, '', 'both');'" works when the "Add new" button is clicked.

I understand that this goes through an AJAX submission, but are all variables in the script, local or $_POST cleared or destroyed during that process? An explanation would be appreciated.

Thanks.

Re: Variables display but empty values sent to database

Unless the Ajax submit specifically restricts variable passing, in general, all $_POST / $_GET elements will be sent in to maintain state with the form at hand and be available globally. Any other variables needed inside of any function must suitably be brought in as arguments or be declared global with it.

Re: Variables display but empty values sent to database

OK apmuthu.

I have inserted the following lines before the submit line:

hidden('link_id',$link_id, 1);
hidden('batch_ref', $batch_ref, 1);
hidden('trans_id', $trans_id, 1);

The $_POST variables now come back correctly upon submit and the new record is inserted correctly into the database.

As a follow-up, since the script is recalled after the submit, the top table needs to reflect the link that was added and the bottom table no longer needs to be displayed. This does not occur and even clearing $_GET['link_id'] does not prevent the bottom table from displaying after the submit.  I imagine something more needs to be done as the display remains unchanged.

Re: Variables display but empty values sent to database

Unless the whole page is refreshed (no need for Ajax then) as in a regular Submit, other parts of the page are generally not refreshed.

Ajax, by it's very purpose of use is intended to address small / localised tag specific changes on the occurrence of some event generally the change in a field value.

Multiple such Ajax updates can be triggered by such a change to simulate a full page refresh but alignments and overflow of content will need to be handled gracefully.

Re: Variables display but empty values sent to database

I am presuming after looking through the code of the 'submit_' functions in /includes/ui/ui_input.inc that if I changed the 'submit' line in my view_links.php to the following:

  `submit_add_or_update_center($selected_id == -1, '', 'false');`

That would cause a regular whole page refresh and not an Ajax one.

If this is true, then in the code that checks `if ($Mode=='ADD_ITEM' ||.....`, I add a line after the `create_new_link` function has been successful, to clear the `$_GET['trans_id']` then when the code to display the bottom table checks that `$_GET` variable, it should be empty.

But this does not occur as the bottom table still displays as if the script was called for the first time with that `$_GET` variable set.

Could you please clarify the process once such a submit is triggered.

Re: Variables display but empty values sent to database

Check stale session variables and the need to clear some $_GET variables and acquire it afresh in context.

There could also be several forms on a page that need to be refreshed whilst Ajax addresses only one form at a time.