...

View Full Version : How to allow users to specify the amount of items on my form?



echeats
03-09-2008, 12:13 AM
I have an order form which had 5 part that repeated themselves to allow a person to order up to 5 different items. I originally did it by having $item1, $quantity1, $type1, $item2, $quantity2...etc hardcoded into both the PHP processor and the HTML form (e.g. values of form elements were item1, quantity1, etc).

I have now re-coded it so it uses arrays for both the HTML form (the parts that repeat are in the array) and the PHP processor, and all is working fine. Both currently use a variable $itemamount = 5 which is hardcoded into the script.

The only thing is, now i have my arrays i was wondering if i can make it so users can somehow input/specify how many items they wish to add? Ideally i don't want to use POST as the form using the arrays has an "if POST" thing that it could mess up. The other slightly problem is that the page is included in a larger CMS type system which means using GET is impossible as you can't get the value into the URL (e.g. by using form.php?itemamount=3)...so i wondered if theres another way of doing this?

Basically i either want a seperate page that asks the user how many items he/she wants to order, which then forwards them onto form.php that will give them the form to fill in. Alternatively, if all the code was on the form page and they (by default) have 1 item field and could then somehow click "Add Item..." if they wished to add more and more as many times as they want?

So, any ideas? I'm a little stuck so appreciate any help!

Inigoesdr
03-09-2008, 08:39 AM
You have several ideas there, any of which would work. The easiest of your suggested options is probably having a separate form with a drop-down with the number of items, and a submit button that said "Update Number of Products" or something similar. You could also use Javascript to dynamically update the page without reloading.

echeats
03-09-2008, 03:09 PM
I'd really like to do the Javascript option (due to the possible problems mentioned with GET and POST above) but am not sure where to start, but i really like the idea of not having to reload the page.

Do you have some code that would allow me to do that? Im not sure where to start.

CFMaBiSmAd
03-09-2008, 03:44 PM
I recommend starting simple, then add features that match your level of programming skills. If you have experience with javascript, then dynamically building the form with the requested number of fields or simply having an "add item" click-able link that adds a row of inputs each time it is clicked is certainly possible.

For your existing code that uses arrays, it is not necessary to carry around a variable that says how many elements there are. Just use a foreach() loop. This will also allow the form processing code to work for any number of input fields without needing to set or change anything to match the number of elements.

For your dynamic number of items, the simplest method, and this will work even if someone has turned off javascript in their browser, would be to have a drop down on your form to pick the quantity. Then have a small submit button (or use the onchange event) to submit this number to your form processing code. Your form processing code would then build and output a form with the requested number of fields.

For the next step up, there are tons of "add form field" javascripts that you can find through searching on the Internet.

echeats
03-09-2008, 06:21 PM
For your dynamic number of items, the simplest method, and this will work even if someone has turned off javascript in their browser, would be to have a drop down on your form to pick the quantity. Then have a small submit button (or use the onchange event) to submit this number to your form processing code. Your form processing code would then build and output a form with the requested number of fields.

Any example code for this part? Google doesn't like me :(

echeats
03-10-2008, 09:45 AM
I've still not had any luck finding the code that would let me do this. I know what needs to happen but just cant write it from scratch as wouldn't know where to start.

Can anyone point me in the right direction?

abduraooft
03-10-2008, 09:55 AM
Any example code for this part? Google doesn't like me :(
When I used the quick search in this forum for the key words dynamic form, I got plenty of results.
http://www.google.com/search?q=site%3Acodingforums.com+dynamic+form&qfront=dynamic+form

echeats
03-10-2008, 12:15 PM
I tried using your code here (http://codingforums.com/showthread.php?t=132080) but didn't have any luck as what that is doing is alot simpler than the fields i am trying to add. I can't find anything that allows me to put a lot of code into the hidden div, most of them are like your's and use JS to add just a few text boxes?

Here's the code i'd like people to be able to add/remove (like in your code)...


<?

for($x = 0; $x < 1; $x++) {
// item
echo ($error['theitem'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
echo '<strong>Item</strong> (*)</td>';
echo '<td><select name="theitem[]">';
if($_POST['theitem'][$x]) echo '<option>' . $_POST['theitem'][$x] . '</option>';
include('products.txt');
echo '</select></td></tr>';

// quantity
echo ($error['quantity'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
echo '<strong>Quantity</strong> (*)</td>';
echo '<td><select name="quantity[]">';
if($_POST['quantity'][$x]) echo '<option>' . $_POST['quantity'][$x] . '</option>';
include('quantity.txt');
echo '</select></td></tr>';

// reason
echo ($error['reason'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
echo '<strong>Reason</strong> (*)<br \>(minimum 10 characters)<br \><br \><br \></td>';
echo '<td><textarea name="reason[]" rows="5" style="width:500px;">';
if($_POST['reason'][$x]) echo $_POST['reason'][$x];
echo '</textarea></td></tr>';

// type
echo ($error['thetype'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
echo '<strong>Type</strong> (*)</td>';
echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Replacement" ';
if($_POST['thetype'][$x][0] == 'Replacement') echo 'checked="checked"';
echo ' />&nbsp;Replacement</td></tr>';
echo '<tr><td> </td>';
echo '<td><input type="radio" name="thetype[' . $x . '][]" value="Enhancement" ';
if($_POST['thetype'][$x][0] == 'Enhancement') echo 'checked="checked"';
echo ' />&nbsp;Enhancement';
echo '</td></tr>';

// oldassets
echo ($error['asset'][$x]) ? '<tr><td style="color: red;">' : '<tr><td>';
echo '<strong>Old assets?</strong></td>';
echo '<td><input type="text" name="asset[]" value="';
if($_POST['asset'][$x]) echo $_POST['asset'][$x];
echo '" style="width:200px;" /> (required if "Replacement" is select above)';
echo '</td></tr>';
echo '<tr><td colspan="2"><hr></td></tr>';
}

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum