Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-24-2007, 09:28 AM   PM User | #1
Kal
Regular Coder

 
Join Date: Dec 2005
Posts: 309
Thanks: 0
Thanked 0 Times in 0 Posts
Kal is an unknown quantity at this point
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
Kal is offline   Reply With Quote
Old 01-24-2007, 03:29 PM   PM User | #2
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
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

aedrin is offline   Reply With Quote
Old 01-31-2007, 09:03 AM   PM User | #3
Kal
Regular Coder

 
Join Date: Dec 2005
Posts: 309
Thanks: 0
Thanked 0 Times in 0 Posts
Kal is an unknown quantity at this point
am i correct to assume that the varaible
PHP Code:
$package 
is the name of the form?
Kal is offline   Reply With Quote
Old 01-31-2007, 09:17 PM   PM User | #4
aedrin
Senior Coder

 
Join Date: Jan 2007
Posts: 1,648
Thanks: 1
Thanked 58 Times in 54 Posts
aedrin will become famous soon enough
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. 
aedrin is offline   Reply With Quote
Old 02-01-2007, 01:03 PM   PM User | #5
Kal
Regular Coder

 
Join Date: Dec 2005
Posts: 309
Thanks: 0
Thanked 0 Times in 0 Posts
Kal is an unknown quantity at this point
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>';
    }

?>
Kal is offline   Reply With Quote
Old 02-01-2007, 01:19 PM   PM User | #6
Kal
Regular Coder

 
Join Date: Dec 2005
Posts: 309
Thanks: 0
Thanked 0 Times in 0 Posts
Kal is an unknown quantity at this point
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?
Kal is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:40 AM.


Advertisement
Log in to turn off these ads.