...

View Full Version : adding form values into array



Kal
01-24-2007, 10:28 AM
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

aedrin
01-24-2007, 04:29 PM
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



session_start();




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





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
}

Kal
01-31-2007, 10:03 AM
am i correct to assume that the varaible
$package is the name of the form?

aedrin
01-31-2007, 10:17 PM
The second $package variable would be a variable you create based upon the data received.

For instance;



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

Kal
02-01-2007, 02:03 PM
i seem to be getting the following error


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

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
02-01-2007, 02:19 PM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum