carlg Posted November 17, 2010 Share Posted November 17, 2010 I'm not sure of the appropriate subject line for this feature, but hopefully I'm close. I'll try to describe what I'm trying to do. The most common place you see this is on web based file uploads so I'll use it as my example. Let's say I want to allow my user to upload files so there is a text box where they enter the file name. The user is allowed to upload as many files as he wants with 1 push of the submit button. Upon the loading of the form, there will be 5 textboxes for file upload, but if the user wants to upload more than 5 files, there is a link that is clicked and I think via ajax or javascript another textbox will be added. The user can do this for as many files he wants to upload. This is what it looks like to the user, but then when the form is submitted how are these variables referenced? So how is this setup in the main form and how are the variables referenced after form submission? If anyone can point me to a good web tutorial on this type of thing I would appreciate it. Thanks Carl Quote Link to comment https://forums.phpfreaks.com/topic/218985-user-generated-selections/ Share on other sites More sharing options...
Psycho Posted November 17, 2010 Share Posted November 17, 2010 First off a file upload field is not a text box - it is a file input that will include a browse button. Anyway, you can simply create your form with however many file input fields as you want. Then, using javascript, provide a method for the user to add more input fields. The key to this is to name the input fields as an array. Then you can reference the fields on the receiving page as an array <input type="file" name="files[]" Quote Link to comment https://forums.phpfreaks.com/topic/218985-user-generated-selections/#findComment-1135647 Share on other sites More sharing options...
carlg Posted November 17, 2010 Author Share Posted November 17, 2010 "First off a file upload field is not a text box - it is a file input that will include a browse button." I knew this already, thanks. File upload is the most common place you see this technique used on the web. In my application, there will be groupings of 3 items (2 dropdowns and 1 text box). So I guess I would handle this in a similar fashion? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/218985-user-generated-selections/#findComment-1135656 Share on other sites More sharing options...
Psycho Posted November 17, 2010 Share Posted November 17, 2010 In my application, there will be groupings of 3 items (2 dropdowns and 1 text box). Not quite sure what you are trying to say there. Are you saying that each upload will consist of several fields: one for the actual file upload and additional fields to describe the upload? If so, then all the fields should be defined as arrays. Example File 1: <input type="file" name="files[]" /> Type: <select name="filetype[]"><option>Avatar</option><option></option>thumb</select> Name: <input type="text" name="file_name" /> File 2: <input type="file" name="files[]" /> Type: <select name="filetype[]"><option>Avatar</option><option></option>thumb</select> Name: <input type="text" name="file_name" /> //etc... Then, on the processing page, to access the fields for the first image you would use: $_POST['files'][0], $_POST['file_type'][0], $_POST['file_name'][0] For the second image: $_POST['files'][1], $_POST['file_type'][1], $_POST['file_name'][1] etc. Quote Link to comment https://forums.phpfreaks.com/topic/218985-user-generated-selections/#findComment-1135736 Share on other sites More sharing options...
carlg Posted November 18, 2010 Author Share Posted November 18, 2010 Yes, very similar to that, thanks. Besides I'm not doing file uploads (I just used file uploads as an example since it is a common place to see this technique used) My application will have student name, grade point average, and major. The user can add as many students as he wishes; Quote Link to comment https://forums.phpfreaks.com/topic/218985-user-generated-selections/#findComment-1136198 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.