Jump to content

Making a form scalable.


poleposters

Recommended Posts

Hi, I have a simple order form. It works fine. But I have a feeling that the code is fairly primitive. Already I can see that adding more items to the order form is going to be more work than it should be.

 

The form is just a series of select menus. The user simply selects the number of items they'd like to purchase. This form data is passed to another form which confirms the order before emailing me so that I can fulfill the order.

 

Heres the code for the confirmation page.Is there a better way to right this code? ie not have to rely on each individual select box. There are nine of them at the moment but I can see the list getting bigger.

<?php

$book=$_POST['book'];
$wardrobe=$_POST['wardrobe'];
$tea=$_POST['tea'];
$archive=$_POST['archive'];
$wrap=$_POST['wrap'];
$tape=$_POST['tape'];
$dispenser=$_POST['dispenser'];
$mattress=$_POST['mattress'];
$butchers=$_POST['butchers'];


ob_start();
print "<h2>You have ordered:</h2>";
print "<ul>";
if($book!=0){print "<li>$book book wine box</li>"; }
if($wardrobe!=0){print "<li>$wardrobe wardrobe box</li>"; }
if($tea!=0){print "<li>$tea tea chest box</li>"; }
if($archive!=0){print "<li>$archive archive box</li>"; }
if($wrap!=0){print "<li>$wrap roll of bubble wrap</li>"; }
if($tape!=0){print "<li>$tape roll of tape</li>"; }
if($dispenser!=0){print "<li>$dispenser tape dispenser</li>"; }
if($mattress!=0){print "<li>$mattress mattress protector</li>"; }
if($butchers!=0){print "<li>$butchers ream of butcher's paper</li>"; }
print "</ul>";
$output=ob_get_clean();


?>

 

Link to comment
https://forums.phpfreaks.com/topic/176645-making-a-form-scalable/
Share on other sites

You could try

 


foreach ($_POST as $k=>$v)
{
switch($k)
	{
	case "book":
		print "$v book  wine box";
		break;

	csae "wardrobe":
		print "$v wardrobe box";
		break;
	}
}

 

And you can add like that. Or have

 

$Array = array();
$Array['wardrobe'] = "wardrobe box";
$Array['book'] = "book wine box";

foreach ($_POST as $k=>$v)
{
if(isset($Array[$k]))
	{
	print $v . " " . $Array[$k];
	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.