Web Analytics Made Easy -
StatCounter Advice on a Very Basic Form Output Script - CodingForum

Announcement

Collapse
No announcement yet.

Advice on a Very Basic Form Output Script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Advice on a Very Basic Form Output Script

    I'm working my way through a TuxRadar tutorial on PHP/MySQL that was recommended by guelphdad in a stickie on MySQL resources here in Coding Forum.

    I know very little about PHP or MySQL. I have tinkered with javascript in the past and I'm pretty well-versed in HTML and CSS.

    Anyway I've begun to learn thanks to TuxRadar. Nevertheless, even their short and sweet step-by-step approach takes a big jump now and again. Right over my head, frankly.

    Thankfully, I have a lot of time on my hands.

    My problem was that the example given in a brief introduction to $_GET and $_POST methods demonstrates the fundamental difference between outputting form data in an array as opposed to sending it along with an url.

    Anyway, the mark-up of my html form goes like this:

    Code:
    <form action="postform.php" method="post">
    
    <p>
    First Name: <input type="text" name="Form[First]" />*
    </p>
    
    <p>
    Last Name: <input type="text" name="Form[Last]" />*
    </p>
    
    <p>
    Age: <input type="text" name="Form[Age]" />
    </p>
    
    <p>
    Password: <input type="password" name="Form[Pass]" maxlength="10" />*
    </p>
    
    <p>
    Life story:
    </p>
    
    <p>
    <textarea name="Story" rows="10" cols="80">Enter your life story here</textarea>
    </p>
    
    <p>
    <input type="radio" name="Form[Sport][]" value="Tennis" /> Tennis
    <input type="radio" name="Form[Sport][]" value="Cricket" /> Cricket
    <input type="radio" name="Form[Sport][]" value="Baseball" /> Baseball
    <input type="radio" name="Form[Sport][]" value="Polo" /> Polo
    </p>
    
    <p>
    Languages:
    </p>
    
    <p>
    <input type="checkbox" name="Form[Languages][]" value="PHP"" /> PHP
    <input type="checkbox" name="Form[Languages][]" value="CPP" /> C++
    <input type="checkbox" name="Form[Languages][]" value="Delphi" /> Delphi
    <input type="checkbox" name="Form[Languages][]" value="Java" /> Java
    </p>
    
    <p>
    * Required fields.
    </p>
    
    <p>
    <input type="submit" />
    </p>
    
    </form>
    The tutorial output script goes like this:

    PHP Code:
    <?php
        
    if (isset($_POST['Form']))
        {
            
    import_request_variables("p""z");
            
    $missingfields = array();
            
    $required = array("First"=>"First Name""Last"=>"Last Name""Pass"=>"Password",);

            while (list(
    $var$val) = each($required))
            {
                if (isset(
    $zForm[$var]) && $zForm[$var] != '')
                {                        {
                    
    // check this value further here
                
    }
                else
                {
                    
    $missingfields[$var] = $val;
                }
            }

            if (
    count($missingfields))
            {
                print 
    "You missed out one or more fields:<br />";

                while(list(
    $var$val) = each($missingfields))
                            {
                print 
    $val "<br />";
            }
        }
        else
        {
            print 
    "Form passed!<br />";
            
    var_dump($zForm['Sport']);
            echo 
    '<br />';
            
    var_dump($zForm['Languages']);
            exit;
            }
        }
    ?>
    The thing is that the tutorial script outputs the selected POST data in a bald array. After looking up var_dump() on the Manual I understand it now but I have to admit, I thought I'd messed up the coding when I glanced at the output the first time.

    I'm that ignorant.

    But it's all good. Finally I'm very clear about GET and POST after years of seeing them referenced in HTML mark-up without actually having to worry about the difference.

    Nevertheless, I wanted to see something I could relate to so I cobbled together my own variant of the tutorial code in order to output the form data in a couple of sentences. It goes like this:

    PHP Code:
    <?php
    // Code from TuxRadar: http://www.tuxradar.com/practicalphp/7/7/3
    // Uses a modified version of 'form.html' from a previous tutorial: 
    // http://www.tuxradar.com/practicalphp/7/3/3
    // The script is modified to output results in a sentence rather than an array

        
    if (isset($_POST['Form']))
        {
            
    import_request_variables("p""z");
            
    $missingfields = array();
            
    $required = array("First"=>"First Name""Last"=>"Last Name""Pass"=>"Password",);

            while (list(
    $var$val) = each($required))
                    {
            if (isset(
    $zForm[$var]) && $zForm[$var] != '')
            {
                
    // check this value further here
            
    }
            else
            {
                
    $missingfields[$var] = $val;
            }
        }
        if (
    count($missingfields))
            {
                print 
    "You missed out one or more fields:<br />";

                while(list(
    $var$val) = each($missingfields))
                {
                    print 
    $val "<br />";
                }
            }
            else
            {
                echo 
    "Form passed!<br />";
                if (isset(
    $zForm['Sport']) && $zForm['Sport'] != '')
                {
                    foreach (
    $zForm['Sport'] as $f)
                    {
                         echo 
    "Your favourite sport is ".$f.".<br />";
                    }
                }
                else
                {
                     echo 
    "You have no favourite sport.<br />";
                }
                if (isset(
    $zForm['Languages']) && $zForm['Languages'] != '')
                {
                    
    $last_f end($zForm['Languages']);
                    
    $secondlast_f prev($zForm['Languages']);
                    echo 
    'You are proficient in';
                    
    $total = (int)count($zForm['Languages']);

                    
    /* If there is only one language selection … */

                     
    if ($total 2)
                    {
                        foreach (
    $zForm['Languages'] as $f)
                        {
                            echo 
    ' '.$f.'.';
                        }
                    }

                    
    /* If there is more than one language selection … */

                    
    else if ($total 1)
                    {
                        foreach (
    $zForm['Languages'] as $f)
                        {

                            
    /* Drop the serial comma … */

                            
    if ($f == $secondlast_f)
                            {
                                echo 
    ' '.$f.' ';
                            }

                            
    /* Add a conjunction before the last language … */

                            
    else if ($f == $last_f)
                            {
                                echo 
    ' and '.$f.'.';
                            }

                            
    /* Finish with a full stop after the last language … */

                            
    else if ($f != $last_f)
                            {
                                echo 
    ' '.$f.',';
                            }
                        }
                    }
                }
                else
                {
                    echo 
    "You have no coding experience.";
                }
                exit;
            }
        }
    ?>
    I have error reporting set to E_ALL & ~E_NOTICE | E_STRICT and it works fine. Still, I'd really appreciate any advice on how I might improve the bit I added (basically that part of the block dealing with output from the Languages array). I would also like to know if there is any method other than a foreach statement to output the Sport array since that will only ever be a single choice via a radio button.

    Note: I use UltraEdit to write code and html mark-up but I've had the devil of a job pasting this into the message field and I've just bent my head restoring the indents. All the formatting was lost. Any suggestions on that score?
    Last edited by Enver; Aug 25, 2011, 05:03 PM. Reason: Changed [CODE] tags to [PHP] tags.
Working...
X
😀
🥰
🤢
😎
😡
👍
👎