Back navigation with preassigned fields

Once the PHP sessions are activated, the fields are no longer automatically preset by the browser, if you'll come back through a notification or other fields to a form.

This must now be programmed itself for each form. However, this also has the advantage that you can better respond to input errors. (For example, the unfilled box yellow or red highlighted, etc.)

Based on the maintenance dialog of the announcements the procedure will now be described:

The first thing you must secure these entries in a session variable at the beginning of announcements_function.php that handles the user input from the form. To not edit now all $_GET or $_POST separate there is a PHP variable $_REQUEST that includes the contents of both arrays and the $_COOKIE array. The name of the $_SESSION variable should be unique per request form and end with _request, so you know what is meant and different forms do not overwrite the data.

 $_SESSION['announcements_request'] = $_REQUEST; 

Now, the contents must be processed in announcements_new.php.

If you work here with a table class, so there is a very easy way to demonstrate the fields again:

 if (isset($_SESSION['announcement_request']))
{
    foreach ($_SESSION['announcement_request'] as $key => $value)
    {
        if (strpos($key, 'ann_') == 0)
        {
            $announcement->setValue($key, stripslashes($value));
        }        
    }
    unset($_SESSION['announcement_request']);
}

What is important in this approach is that every box on the form has the name given to the database field names! Otherwise, the assignment will not work correctly.

When working without Table access class must be removed the following:

For this purpose, you should create best practice for each field on the form a variable. If now $_SESSION ['announcements_request'] exists, the variable can be filled with the old content, otherwise they will simply initialized.

 if (isset($_SESSION['announcements_request']))
{
	$formValues = strStripSlashesDeep($_SESSION['announcements_request']);
	unset($_SESSION['announcements_request']);
}
else
{
	$formValues['headline']    = ' ';
	$formValues['description'] = ' ';
	$formValues['global']      = 0;	
	if ($_GET['ann_id'] != 0)
	{
	... Reading the id - dataset and preassign the $field variable with the data from the database ...
	}
}

Now you can later easily prove the form fields with the appropriate variables.

However, you have to take in account, when to clear the variable $_SESSION['announcements_request'], otherwise each time you call from announcements_new.php the fields of these variables are preassigned. It makes sense that, once the new or amended notice has been saved. So in announcements_function.php. But as soon as you navigate back and the variable has been read, they should be deleted

 unset($_SESSION['announcements_request']);. 

For security reasons but you should also still delete in announcements.php because creating or editing can also be canceled by the user.

  • en/entwickler/zuruecknavigieren_mit_vorbelegten_feldern.txt
  • Last modified: 2016/12/03 15:03
  • by ximex