SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy need help with form/submission pages

    So I am trying to build this pageI Want to Have Tickets.zip in PHP.
    I am at a loss for how to proceed. I know I need to create a submit page also, but I don't know how to do that.
    Here is a link to my form page code so far: http://pastebin.com/K6GdgVmQ
    Any help you experts out there could offer me would be highly appreciated.
    Not as concerned about the CSS for the pictures as I am with the functionality of the PHP.
    -phpnoob91

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    So... what do you want it to do?
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First off you need to place all form fields inside the form itself. And all form data will be available from the $_POST array not inside the $_SESSION array.

    I have modified your example to display the first group of radio buttons with a loop inside the form. You should only have to modify it slightly to get it to work with select boxes. Hope this helps you get started.

    PHP Code:
    <?php

    // if there was information submitted then process it.
    if( isset( $_POST ) && !empty( $_POST ) )
    {
        
    // process $_POST data
        
    echo 'Contents of $_POST array:';
        echo 
    '<pre>' print_r$_POST) . '</pre>';
    }

    $ticket_quality = array(
        
    'Best' => 'Give me the best seats you have got - $110.00',
        
    'Not_lame' => 'Give me some seats that wont make me feel lame - $75.00',
        
    'Desperate' => 'Ill take any solid slab I can park my butt on. Or, Ill stand - $25.00',
    );
    $nice_amenities = array(
        
    'Drink' => 'Medium Fountain Drink - $1.50',
        
    'Side' => 'One Delicious Side of Fries or Onion Rings - $1.50',
        
    'Pin' => 'A Tiny but Cool Pin with which to Show My Fan Loyalty - $1.50',
        
    'Sticker' => 'A Sticker Suitable for a 3-Ring Binder or the Top of a Laptop - $1.50',
        
    'Earplugs' => 'Designer Earplugs - $1.50'
    );
    $best_amenities = array(
        
    'Combo' => 'One of those Combo Things Where you Get a Sandwich, Fries, and a Drink - $7.00',
        
    'Cd' => 'A CD of the Concert, Available within Seconds of the Concerts Ending - $7.00',
        
    'Frisbee' => 'A Frisbee with a Hallucinogenic Design Only Visible from a Helicopter - $7.00',
        
    'Shirt' => 'A T-Shirt that Looks Suspiciously Worn - $7.00',
        
    'Picture' => 'A Shot of Me Screaming in Slow Motion on the Jumbotron - $7.00'
    );

    ?> 
    <html>
        <head>
            <title>Final Form</title>
        </head>
        <body>
            <div id="wrapper">
                <div id="container">
                    <div id="content">
                        <div id="masthead">
                            Masthead
                        </div>        
                        <form action="" method="POST">
                            <h2>I have tickets of a certain quality</h2>
                            <p>
                                <?php
                                    
    // create a loop to cycle through all ticket quality items
                                    
    foreach( $ticket_quality as $key => $value 
                                    {
                                        echo 
    "<input type='radio' name=ticket_quality'
                                            value='
    {$key}'> {$value} </input><br />";
                                    }
                                
    ?>
                            </p>
                            
                            <h2>I Want To Tell You Who I Am and Where I Live</h2>
                            <table>
                                <tr>
                                    <td><label for="full_name">Full Name</label></td>
                                    <td><input type="text" name="full_name" size=30></td>
                                </tr>
                                <tr>
                                    <td><label for="street_address">Street Address</label></td>
                                    <td><input type="text" name="street_address" size=30></td>
                                </tr>
                                <tr> 
                                    <td><label for="zip_code">Zip Code</label></td>
                                    <td><input type="text" name="zip_code" size=30></td>
                                </tr>
                                <tr>
                                    <td><label for="email_address">Email Address</label></td>
                                    <td><input type="text" name="email_address" id="email_address" size="30"></td>
                                </tr>
                                <tr>
                                    <td><label for="phone">Phone</label></td>
                                    <td><input type="text" name="phone" size=30></td>
                                </tr>
                                <tr>
                                    <td><input type="submit" value="continue"></td>
                                    <td></td>
                                </tr>
                            </table>
                        </form>
                    </div>
                </div>
            </div>
        </body>
    </html>

  4. #4
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    So... what do you want it to do?
    I'm so over my head. Big thanks to all responses so far.
    this is what needs to happen:
    1) Collect all submitted data in a logical, useable way.
    2) Validate the submitted data. Required fields include the ticket "quality" and all of the name/address information. Amenities are not required.
    3) Send the user back to the form if there are any errors. Output all error information and populate the form fields with the data previously entered - this includes radio buttons and check boxes.
    4) If the submission is valid, calculates the user's total based on the ticket quality and amount of amenities ordered. Add a 5% surcharge and 6% sales tax.
    5) Show the user all submitted data plus total(s) for confirmation.

  5. #5
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Time to expand on your steps.

    Quote Originally Posted by phpnoob91 View Post
    1) Collect all submitted data in a logical, useable way.
    This is governed by your form's HTML.
    2) Validate the submitted data. Required fields include the ticket "quality" and all of the name/address information. Amenities are not required.
    Define your validation rules for each field.
    3) Send the user back to the form if there are any errors. Output all error information and populate the form fields with the data previously entered - this includes radio buttons and check boxes.
    This will require modification of your HTML form once you're finished with it, and obviously will rely on your validation rule definitions.
    4) If the submission is valid, calculates the user's total based on the ticket quality and amount of amenities ordered. Add a 5% surcharge and 6% sales tax.
    Some simple math. Based on your example file, you posess sufficient skill to do arithmetic operations.
    5) Show the user all submitted data plus total(s) for confirmation.
    Simple regurgitation. Again, this falls within your skillset based on the above file.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  6. #6
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Define your validation rules for each field.
    Is this done in the submission page only?
    This will require modification of your HTML form once you're finished with it, and obviously will rely on your validation rule definitions.
    What sort of modifications?
    Some simple math. Based on your example file, you posess sufficient skill to do arithmetic operations.
    While I understand the math is simple, I don't have a strong grasp on the PHP syntax to do this well. Would this be done on the submission page too? If so, where?
    Simple regurgitation. Again, this falls within your skillset based on the above file.
    Simple regurgitation...okay. But some tips for how to go about this would be very helpful

    Was having some difficulty with the multiquote feature so I responded this way. Thanks for your patience everybody. I'm not the most computer literate person, clearly!

  7. #7
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    which form fields aren't inside the form itself?
    I thought all the info in the file was part of the form...

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2011
    Posts
    70
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by phpnoob91 View Post
    which form fields aren't inside the form itself?
    I thought all the info in the file was part of the form...
    Only fields that are inside the <form></form> tags are considered to be inside the form.

    I have attached an example of how to do a very simple form validation. It's a bit quick and dirty but it might help you understand how to get a start on what you need to do.

    Any value that is inside the form is accessible from within the $_POST array. If you have a text box with a name of FirstName, like this:
    HTML Code:
    <input type="text" name="FirstName" />
    It will be be available in the $_Post array like this:
    PHP Code:
    $firstName $_POST['FirstName']; 
    The name element in the html is used as the index in the $_POST array.

    PHP Code:
    <?php
    /*
     * If there is any data submitted in the form it will exist in the $_POST array.
     * The first thing is to check to see if it is empty and act accordingly.
     */

    // initialize variable to hold form data and any errors
    $person = array();
    $errors = array();
    // check Post Array
    if( !empty( $_POST ) )
    {
        
    // we have established that the array is not empty so we can begin validation.
        
        /**
         * Check to see if firstName was entered and make sure it's not empty.
         * These are just the validation conditions that I chose, but you can verify
         * it anyway you want.
         */
        
    if( isset( $_POST['firstName'] ) && $_POST['firstName'] !== '')
        {
            
    // passed validation
            
    $person['first'] = $_POST['firstName'];
        }
        else
        {
            
    // there was an error somewhere
            
    $errors[] = 'You must enter a first name';
        }
        
        if( isset( 
    $_POST['lastName'] ) && $_POST['lastName'] !== '' )
        {
            
    // passed validation
            
    $person['last'] = $_POST['lastName'];
        }
        else
        {
            
    // there was an error
            
    $errors[] = 'You must enter a last name';
        }
        
        
    // if the erros array is empty then handle data, otherwise continue and display
        // form.
        
    if( empty( $errors ) )
        {
            
    // no errors occured and it is safe to submit data.
            
            // handle data and then redirect user to a success page or wherever you want
            // them to go.
            
    header'Location: success.php');
        }
    }

    ?>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title></title>
        </head>
        <body>
            
            <form action="" method="post">
                <?php
                    
    /**
                     * if $errors array is not empty then display any error messages.
                     */
                    
    if( !empty( $errors ) )
                    {
                        foreach( 
    $errors as $error )
                        {
                            echo 
    "<p>{$error}</p>";
                        }
                    }
                
    ?>
                <p>
                    <label for="firstName">Enter First Name:</label><br />
                    <input type="text" name="firstName" 
                           <?php 
                           
    /**
                            * This will display any information in the fields that 
                            * passed validation because it was passed into the 
                            * $person array.
                            */
                           
    if( isset( $person['first'] ) )
                           {
                               echo 
    "value='{$person['first']}'"
                           }
                           
    ?> />
                </p>
                <p>
                    <label for="lastName">Last Name:</label><br />
                    <input type="text" name="lastName" 
                           <?php if( isset( $person['last'] ) )
                           {
                               echo 
    "value='{$person['last']}'"
                           } 
                           
    ?> />
                </p>
                <p>
                    <input type="submit" value="Submit" />
                </p>
                
            </form>
        </body>
    </html>
    Was this helpful at all?

  9. #9
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,747
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by phpnoob91 View Post
    Is this done in the submission page only?
    No this is done logically, not in the code (at first). Make a list of your fields, and then next to them what validation (if any) needs to be applied to it. Then turn your validation rules into code.
    What sort of modifications?
    Filling the form fields with previous values. value='' is still valid.
    While I understand the math is simple, I don't have a strong grasp on the PHP syntax to do this well. Would this be done on the submission page too? If so, where?
    If you're using seperate submission/handler pages, the math can only be done on the handler page. The submission page would not know in advance what a user will select.
    Simple regurgitation...okay. But some tips for how to go about this would be very helpful
    Jeremy has answered this one with his example above on how to get at the information sent.

    Was having some difficulty with the multiquote feature so I responded this way. Thanks for your patience everybody. I'm not the most computer literate person, clearly!
    I do try not to hand people code and make them do it themselves (or at least try, and then supply tweaks), as it helps most people learn.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.


Bookmarks

Posting Permissions

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