Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    How to allow users to specify the amount of items on my form?

    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!

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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.

  • #3
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,043
    Thanks
    2
    Thanked 316 Times in 308 Posts
    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.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #5
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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

  • #6
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Quote Originally Posted by echeats View Post
    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%...t=dynamic+form
    Last edited by abduraooft; 03-10-2008 at 12:04 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #8
    New Coder
    Join Date
    Mar 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I tried using your code here 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)...

    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>';
    }
    
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •