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 6 of 6
  1. #1
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts

    adding form values into array

    i have a question.

    i have a form, which has triple drop down lists which populate depending on the previous selection using javascript, an input text box, and a checkbox.

    the purpose of this form is to allow a user to create a phone package, now a user can create more than one package, however only one form is displayed, is there a way i can submit one form into an array and display that underneith on the same page and blank the form to add another package.

    and then submit all the packages to be stored in a mysql database.

    thanks in advance

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    It should be fairly easy.

    Have your Javascript generated form submit to a PHP file (most likely the same one that generates the original form).

    Use sessions to store the package in a temporary variable. Print out the contents of that variable at the bottom of the page when it is being generated.

    Then have another button to "Finish" the process, which goes through the session variable and submits it into MySQL.

    Somewhat like this:

    package.php

    PHP Code:
    session_start(); 
    Code:
    <form action="package.php">
    ( Your Form )
    <input type="submit" name="add" />
    <input type="submit" name="finish" />
    </form>
    PHP Code:

    if (isset($_REQUEST['finish'])) {
        foreach (
    $_SESSION['packages'] as $package) {
            
    // TODO: Use MySQL to insert each package into the Database
            
    echo "Your order has been received!";
        }
    }

    if (isset(
    $_REQUEST['add'])) {
        
    // TODO: Create an array with the phone package here
        
    $_SESSION['packages'][] = $package;
    }

    foreach (
    $_SESSION['packages'] as $package) {
        
    // TODO: Echo your package details as HTML


  • #3
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    am i correct to assume that the varaible
    PHP Code:
    $package 
    is the name of the form?

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    The second $package variable would be a variable you create based upon the data received.

    For instance;

    PHP Code:
    $package = array();
    $package['name'] = $_REQUEST['name'];
    $package['total'] = $_REQUEST['total'];
    // etc. 

  • #5
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i seem to be getting the following error

    Code:
    Warning: Invalid argument supplied for foreach()
    i have tried putting the whole of the second statement in brackets but then for each form set of results you put in the array if displays several times.

    PHP Code:
    <?php
        
    if (isset($_REQUEST['finish'])) 
        { 
        foreach (
    $_SESSION['packages'] as $package
            { 
            
    // TODO: Use MySQL to insert each package into the Database 
            
    echo "Your order has been received!"
            } 
        } 

    if (isset(
    $_REQUEST['add']))
        { 
        
    // TODO: Create an array with the phone package here 
        
    $package = array(); 
        
    $package['deal_type_main'] = $_REQUEST['deal_type_main']; 
        
    $package['line_type'] = $_REQUEST['line_type']; 
        
    $package['pacakge_type'] = $_REQUEST['package_type']; 
        
    $package['cli'] = $_REQUEST['cli'];
        
    $package['like_for_like'] = $_REQUEST['like_for_like'];
        
    $package['bus_res'] = $_REQUEST['bus_res'];

        
    $_SESSION['packages'][] = $package
        } 

    foreach (
    $_SESSION['packages'] as $package
        { 
        
    // TODO: Echo your package details as HTML 
        
    print'<table>';

        print
    '<tr>';
            print
    '<td class="subheading">Deal Type</td><td class="maintext">'.$deal_type_main $_POST['deal_type_main'].'</td>';
        print
    '</tr>';

        print
    '<tr>';
            print
    '<td class="subheading">Line Type</td><td class="maintext">'.$line_type $_POST['line_type'].'</td>';
        print
    '</tr>';

        print
    '<tr>';
            print
    '<td class="subheading">Package Type</td><td class="maintext">'.$package_type $_POST['package_type'].'</td>';
        print
    '</tr>';

        print
    '<tr>';
            print
    '<td class="subheading">CLI</td><td class="maintext">'.$cli $_POST['cli'].'</td>';
        print
    '</tr>';

        print
    '<tr>';
            print
    '<td class="subheading">Like for Like</td><td class="maintext">'.$like_for_like $_POST['like_for_like'].'</td>';
        print
    '</tr>';

        print
    '<tr>';
            print
    '<td class="subheading">Business or Residential</td><td class="maintext">'.$bus_res $_POST['bus_res'].'</td>';
        print
    '</tr>';

        print
    '</table>';
        }

    ?>

  • #6
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    just fixed the problem, i wasn't actualy display the values in the array as i was echoing the posted values.

    is there a away in which i can delete a package that is in the array, so when a package is added and displayed, a delete button is also display which allows me to remove that package, and then begin adding others?


  •  

    Posting Permissions

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