Local Hero
05-29-2005, 07:39 PM
I have been working on a long script (for me it's long), and I have one last thing. I have php that creates 3 input fields. Quantity, Color, and Size. The user can add another row of the same options as many times as they need. Each time they do, it also creates a box in another form telling them what they wanted, i.e. (4) - Red - Large. Check it out at http://www.localheroclothing.com/testfiles/Products/phpadd.php
Right now my problem code is similar to this:
var items = '(' + (document.forms.formjunk.amount1.value) + ')' + ' - ' + (document.forms.formjunk.size1.value) + ' - ' + (document.forms.formjunk.color1.value);
var items1 = '(' + (document.forms.formjunk.amount2.value) + ')' + ' - ' + (document.forms.formjunk.size2.value) + ' - ' + (document.forms.formjunk.color2.value);
var items2 = '(' + (document.forms.formjunk.amount3.value) + ')' + ' - ' + (document.forms.formjunk.size3.value) + ' - ' + (document.forms.formjunk.color3.value);
document.forms.orderform_16180.something.value = items;
document.forms.orderform_16180.items1.value = items1;
document.forms.orderform_16180.items2.value = items2;
It's really messy. It also won't work unless all three inputs are available, and won't allow for infinate inputs.
Isn't there an easy way to display the value of each row in infinatly possible textboxes? Here is the entire code:
<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
</HEAD><BODY>
<script type="text/javascript">
function addme() {
var qtytotal = 0, whites = 0, large = 0;
var fields = document.forms["formjunk"].elements;
for (var i=0;i<fields.length;i++) {
if (fields[i].name.indexOf("amount") == 0) {
qtytotal += new Number(fields[i].value);
// This assumes that the select element comes right after the text box
if (fields[i+1].selectedIndex == 0) {
whites += new Number(fields[i].value);
}
if (fields[i+2].selectedIndex == 2) {
large += new Number(fields[i].value);
}
}
}
document.forms.orderform_16180.total.value = qtytotal;
document.forms.orderform_16180.lightshirts.value = whites;
document.forms.orderform_16180.bigshirts.value = large;
var items = '(' + (document.forms.formjunk.amount1.value) + ')' + ' - ' + (document.forms.formjunk.size1.value) + ' - ' + (document.forms.formjunk.color1.value);
var items1 = '(' + (document.forms.formjunk.amount2.value) + ')' + ' - ' + (document.forms.formjunk.size2.value) + ' - ' + (document.forms.formjunk.color2.value);
var items2 = '(' + (document.forms.formjunk.amount3.value) + ')' + ' - ' + (document.forms.formjunk.size3.value) + ' - ' + (document.forms.formjunk.color3.value);
document.forms.orderform_16180.something.value = items;
document.forms.orderform_16180.items1.value = items1;
document.forms.orderform_16180.items2.value = items2;
}
</SCRIPT>
<form name="formjunk" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
$rowsx=1;
if(isset($_POST["rowsx"])){
$rowsx=$_POST["rowsx"];
}
for($e=1;$e<=$rowsx;$e++){
?>
Quantity
<INPUT name="amount<?php echo $e ?>" type="text" id="amount<?php echo $e ?>" onBlur="addme();">
Color<SELECT name="color<?php echo $e ?>" onChange="addme();">
<OPTION value="white">white</OPTION>
<OPTION value="black">black</OPTION>
<OPTION value="red">red</OPTION>
</SELECT>
Size<SELECT name="size<?php echo $e ?>" onChange="addme();">
<OPTION value="small">small</OPTION>
<OPTION value="med">med</OPTION>
<OPTION value="large">large</OPTION>
</SELECT>
<BR>
<?php
}
?>
<BR>
<input type="hidden" name="rows" value="<?php echo $rows+1; ?>" />
<input type="hidden" name="rowsx" value="<?php echo $rowsx+1; ?>" />
<input name="submit" type="submit" value="Add another row" />
</FORM>
<FORM name="orderform_16180">
<INPUT name="something" type="text" id="something"><br>
<?php
$rows=0;
if(isset($_POST["rows"])){
$rows=$_POST["rows"];
}
for($e=1;$e<=$rows;$e++){
?>
<INPUT name="items<?php echo $e ?>" type="text" id="items<?php echo $e ?>"><br>
<?php
}
?>
Total - <INPUT name="total" type="text" id="total">
White - <INPUT name="lightshirts" type="text" id="lightshirts">
Large - <INPUT name="bigshirts" type="text" id="bigshirts">
</FORM>
</BODY>
</HTML>
Thanks :thumbsup:
Right now my problem code is similar to this:
var items = '(' + (document.forms.formjunk.amount1.value) + ')' + ' - ' + (document.forms.formjunk.size1.value) + ' - ' + (document.forms.formjunk.color1.value);
var items1 = '(' + (document.forms.formjunk.amount2.value) + ')' + ' - ' + (document.forms.formjunk.size2.value) + ' - ' + (document.forms.formjunk.color2.value);
var items2 = '(' + (document.forms.formjunk.amount3.value) + ')' + ' - ' + (document.forms.formjunk.size3.value) + ' - ' + (document.forms.formjunk.color3.value);
document.forms.orderform_16180.something.value = items;
document.forms.orderform_16180.items1.value = items1;
document.forms.orderform_16180.items2.value = items2;
It's really messy. It also won't work unless all three inputs are available, and won't allow for infinate inputs.
Isn't there an easy way to display the value of each row in infinatly possible textboxes? Here is the entire code:
<HTML>
<HEAD>
<TITLE>Untitled Document</TITLE>
</HEAD><BODY>
<script type="text/javascript">
function addme() {
var qtytotal = 0, whites = 0, large = 0;
var fields = document.forms["formjunk"].elements;
for (var i=0;i<fields.length;i++) {
if (fields[i].name.indexOf("amount") == 0) {
qtytotal += new Number(fields[i].value);
// This assumes that the select element comes right after the text box
if (fields[i+1].selectedIndex == 0) {
whites += new Number(fields[i].value);
}
if (fields[i+2].selectedIndex == 2) {
large += new Number(fields[i].value);
}
}
}
document.forms.orderform_16180.total.value = qtytotal;
document.forms.orderform_16180.lightshirts.value = whites;
document.forms.orderform_16180.bigshirts.value = large;
var items = '(' + (document.forms.formjunk.amount1.value) + ')' + ' - ' + (document.forms.formjunk.size1.value) + ' - ' + (document.forms.formjunk.color1.value);
var items1 = '(' + (document.forms.formjunk.amount2.value) + ')' + ' - ' + (document.forms.formjunk.size2.value) + ' - ' + (document.forms.formjunk.color2.value);
var items2 = '(' + (document.forms.formjunk.amount3.value) + ')' + ' - ' + (document.forms.formjunk.size3.value) + ' - ' + (document.forms.formjunk.color3.value);
document.forms.orderform_16180.something.value = items;
document.forms.orderform_16180.items1.value = items1;
document.forms.orderform_16180.items2.value = items2;
}
</SCRIPT>
<form name="formjunk" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
$rowsx=1;
if(isset($_POST["rowsx"])){
$rowsx=$_POST["rowsx"];
}
for($e=1;$e<=$rowsx;$e++){
?>
Quantity
<INPUT name="amount<?php echo $e ?>" type="text" id="amount<?php echo $e ?>" onBlur="addme();">
Color<SELECT name="color<?php echo $e ?>" onChange="addme();">
<OPTION value="white">white</OPTION>
<OPTION value="black">black</OPTION>
<OPTION value="red">red</OPTION>
</SELECT>
Size<SELECT name="size<?php echo $e ?>" onChange="addme();">
<OPTION value="small">small</OPTION>
<OPTION value="med">med</OPTION>
<OPTION value="large">large</OPTION>
</SELECT>
<BR>
<?php
}
?>
<BR>
<input type="hidden" name="rows" value="<?php echo $rows+1; ?>" />
<input type="hidden" name="rowsx" value="<?php echo $rowsx+1; ?>" />
<input name="submit" type="submit" value="Add another row" />
</FORM>
<FORM name="orderform_16180">
<INPUT name="something" type="text" id="something"><br>
<?php
$rows=0;
if(isset($_POST["rows"])){
$rows=$_POST["rows"];
}
for($e=1;$e<=$rows;$e++){
?>
<INPUT name="items<?php echo $e ?>" type="text" id="items<?php echo $e ?>"><br>
<?php
}
?>
Total - <INPUT name="total" type="text" id="total">
White - <INPUT name="lightshirts" type="text" id="lightshirts">
Large - <INPUT name="bigshirts" type="text" id="bigshirts">
</FORM>
</BODY>
</HTML>
Thanks :thumbsup: