Jump to content

[SOLVED] Keeping values in forms


WRXHokie

Recommended Posts

I'm trying to process a form.  In my data validation function i try and check for errors in the user submitted fields. In the fields in which are fine i want to be able to keep the values in the form when there are errors and clear the fields that have errors in them.  For some reason i cannot get this to work.  Here's my code, i've cut it down to the relevant parts.  The first file is the index.php file which shows the form but first checks for errors.  The second file has all the relevant php functions i call defined.  In form.php i just show a small piece of the show form function where i try and present a text field with its default value. 

 

<?php

require "form.php";

$defaults = array();

 

if ($_POST['_submit_check']) {

    // If validate_form() returns errors, pass them to show_form()

    if ($form_errors = validate_form($defaults)) {

        show_form($form_errors, $defaults);

    } else {

        process_form();

    }

} else {

    show_form();

}

 

?>

 

 

form.php:

 

<?php

 

function show_form($errors = '', $defaults) {

 

print '<input type="text" name="person" size="1" maxlength="2" value=';

print $defaults[person];

print '>';

 

}

 

 

function process_form() {

    //print "Hello, ". $_POST['my_name'];

    //TODO: MySQL statements to add to database

    show_form();

}

 

function validate_form($defaults = '') {

    // Start with an empty array of error messages

    $errors = array();

    $role_array = array();

 

    if (!is_numeric($_POST['person'])) {

$defaults[person] = "";

        $errors[] = 'Please enter a valid person id (numeric)';

    }

    else {

    $defaults[person] = $_POST['person'];

}

 

 

    if(!isset($_POST['production'])) {

    $errors[] = 'Please select a production';

}

 

 

if(isset($_POST['role'])) {

$role_array = $_POST['role'];

}

else

$errors[] = 'Please select at least one role';

 

    // Return the (possibly empty) array of error messages

    return $errors;

}

 

?>

 

I'm lost, so what am i missing?

 

Link to comment
https://forums.phpfreaks.com/topic/44270-solved-keeping-values-in-forms/
Share on other sites

umm i think you need the quotes try changing the fallowing

print '<input type="text" name="person" size="1" maxlength="2" value=';
print $defaults[person];
print '>';

to

print "<input type='text' name='person' size='1' maxlength='2' value='".$defaults['person']."'>";

tried that... didnt work.  For some reason it isnt keeping the value of $defaults['person'] in tact when it transitions from one function to another.  Its storing it correctly, i can display it in the validate function... but its not there when i get back to the show form function... i'm still at a loss as to why.

Code as it stands now:

 

form.php:

<?php

global $defaults

function show_form($errors = '') {

print '<input type="text" name="person" size="1" maxlength="2" value="';
print "$defaults[person]";
print '">';

}

function process_form() {
     //TODO: MySQL statements to add to database
    show_form();
}

function validate_form() {
    // Start with an empty array of error messages
    $errors = array();
    $role_array = array();

    if (!is_numeric($_POST['person'])) {
        $defaults[person] = "";
        $errors[] = 'Please enter a valid person id (numeric)';
    }
    else {
       $defaults[person] = $_POST['person'];
   }


    if(!isset($_POST['production'])) {
       $errors[] = 'Please select a production';
   }


   if(isset($_POST['role'])) {
      $role_array = $_POST['role'];
   }
   else
      $errors[] = 'Please select at least one role';

    // Return the (possibly empty) array of error messages
    return $errors;
}
?>


index.php:

<?php
require "form.php";

if ($_POST['_submit_check']) {
    // If validate_form() returns errors, pass them to show_form()
    if ($form_errors = validate_form()) {
        show_form($form_errors);
    } else {
        process_form();
    }
} else {
    show_form();
}

?>

 

I'm trying to use a global variable outside the scope of the function definitions and then reference that global variable inside the functions but for some reason it still isnt recognized by the html as a default value.  Any other ideas?

In here....

 

if ($form_errors = validate_form()) {
        show_form($form_errors);
    } else {
        process_form();
    }

 

When you call process_form() it just calls show_form() which displays the form again. Why would you expect the last posted results to still be there? Im sorry, but Im finding it hard to see your logic here.

Alright let me explain each function and its purpose:

 

show_form(): Displays the html based form

 

validate form(): Validates the user entered data in the form. Thus if something is entered wrong, i want to clear that field, but keep all the correctly entered fields in some variable (in this case $default) for displaying when the form gets reprinted with the indicated errors.

 

process form(): This will eventually be mySQL statements to add user entered data into the database.  This function only gets called if the user entered data passes the validate form() with no errors. 

 

I want to be able to keep the correctly entered fields when a form is submitted, but errors are detected.  Then re-display the form with those correct fields still present, and the fields with errors cleared. 

 

With $defaults, i'm trying to store those correctly entered fields.  Making $defaults global in form.php does not work. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.