Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Keep values in HTML form with PHP

    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 Code:
    <?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() {
         
    //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?
    Last edited by WRXHokie; 03-26-2007 at 04:58 PM.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    In the future please wrap your code with [ PHP ] and [ /PHP ] tags (remove the spaces). Much easier to read for us.

    Re: your problem at hand, it looks like you have a scope issue. When you use a variable inside a function, it is accessable to only that function unless you declare the variable as "global" inside the function. So where you are assigning a value to $defaults['person'] inside your validate_form() function, that value's not making it out of the function. Add the line "global $defaults;" at the top of the function to solve this immediate problem, but going down the road you will want to design your code in a way that minimizes (or completely eliminates) global variables, as they make code less usable for a number of reasons.

  • #3
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ah... that make sense.

    If i define it in just that file (form.php) outside the scope of any function and then add values within the functions, will it keep its values from function to function?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    If i define it in just that file (form.php) outside the scope of any function and then add values within the functions, will it keep its values from function to function?
    Not without the "global" statement. The variable inside the function, although named the same, does not point to the same address(es) in memory as the variable outside the function.

  • #5
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help man... but it didnt work for some reason.

    Still not keeping the value even with it defined as global inside form.php

    Here's the code as it stands now:

    PHP Code:
    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();
    }

    ?>

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    want to be able to keep the values in the form when there are errors and clear the fields that have errors in them.
    This would be very frustrating as a user.

    How would I know what I did wrong (even when told what the problem was), if I don't see the input again?

  • #7
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by aedrin View Post
    This would be very frustrating as a user.

    How would I know what I did wrong (even when told what the problem was), if I don't see the input again?
    No i want to clear the fields that have errors and keep the fields that dont have errors.

  • #8
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Put the global statement inside every function you need access to the global variable in. And don't forget the semi-colon

    PHP Code:
    function validate_form() {
        global 
    $defaults;
        .
        .
        .

    p.s. What Aedrin means is it would be better to alert the user when something wrong has been entered, but leave the wrong stuff in the field so the user can see what was entered... just makes it easier for the user to understand what he did wrong.

  • #9
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    Put the global statement inside every function you need access to the global variable in. And don't forget the semi-colon

    PHP Code:
    function validate_form() {
        global 
    $defaults;
        .
        .
        .

    p.s. What Aedrin means is it would be better to alert the user when something wrong has been entered, but leave the wrong stuff in the field so the user can see what was entered... just makes it easier for the user to understand what he did wrong.

    I understand what your saying about leaving the wrong stuff in the field, but thats not standard practice when you enter data into a form that is incorrect. Every form i've ever used deletes the data from the fields that have been incorrectly filled in and then displays how to correctly enter that data in that field. I think it would be counter intuitive to do it the opposite way.

  • #10
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks man, that worked.

  • #11
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    but thats not standard practice when you enter data into a form that is incorrect.
    I've never encountered a form that removes incorrect data I entered.

    Imagine, typing in a 20 digit account number. You forget the dash after the 3rd number. Oops, have to type it all in again!

    Still sound counter intuitive to remove all the data that is incorrect?

    The ONLY exception to this is passwords. But passwords should never be repeated in a form. Even if they are correct.

    Put the global statement inside every function you need access to the global variable in. And don't forget the semi-colon
    The global statement should be avoided whenever possible. It's bad for maintenance and pollutes the global variable space.
    Last edited by aedrin; 03-26-2007 at 07:48 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •