Form submit problem

Permabanned
Joined
25 Oct 2004
Posts
9,078
Hiya,

Got a form all set up with the final step allowing someone to either submit the data or addmore.

Add more is intended to allow them to go back to page 1 of the form and fill in the data all again. So if they fill it in 2-3 or 4 times the data is stored 2-3 or 4 times.

Essentially creating an array(form submissions) of arrays(each forms data). Q

Question is, besides submitting the data once, what would be the best way of ensuring that the array of data is kept in tact while allowing them to input data multiple times.

Think of it as a table with rows and columns, form 1 is row 1, by clicking add more, they enter the same data in row 2 etc.

Im stuck so any help would be greatly appreciated.
 
If you don't want to submit until the very end, you could use javascript to append additional rows to the table. This can be handled entirely locally in the browser or by using Ajax.
 
The data itself isnt a table, its a form, with numerous text inputs, checkbox's, radio buttons and textarea's.

I just used a basic comparison of my form = the columns in a table and each form filled out = a row in that table.

If thats what you meant also, any tips or ideas on how that can be done as my javascript knowledge is very limited.
 
Whatever the container is for your form, a div, a tr, a td, whatever, you just need to either clone it using javascript then append it to the end (with values reset) or use ajax to print out the new form at the end of the previous one.

I would recommend just having one form tag wrapping all the fields.

Google "Javascript clone element" or "Javascript add another". Here's a very simple example to get you started:

http://www.randomsnippets.com/2008/02/21/how-to-dynamically-add-form-elements-via-javascript/
 
How would that translate to php, as i in how would the end result of multiple forms of data appear in php ?

Would it just one big array of data or would it be an array of arrays.

IE.

Array 0 => Array Form 1, Array 1 => Array Form 2 etc
 
You can use arrays or you can prefix each field id/name with the instance number and keep a track of the number of instances on the screen using a hidden form field.

If using prefixes, simply loop through each instance to num_instances to get the data.

I got to say though none of this is how I'd prefer to do it. I would submit the form each time the user clicks on "Save and add another" and save the previous record while creating the form for an additional record.
 
Send the form data back to the page itself and check if the form field has been filled out by checking if the corresponding POST variable is set.

Then pre-fill out the fields that HAVE been submitted with the POST variables that HAVE been set. Then display an error message for any field that is missing/wrong.

example:

$name = $_POST['name'];

if (!$name){echo "<strong><font color=\"red\">Error:</font> Please enter your name.</strong><br />";}

Then pre-fill with the POST data, if it's not set it will be blank:

<label for="name">Your name: </label>
<input id="name" name="name" class="text" value="<?php echo $name; ?>" />
 
Using hidden input boxes wouldnt be a massive drain on resources as a user could potentially input thousands of rows of data, so another method would need to be used.

Im guessing php sessions ?

While i've used sessions already and that works perfectly fine when using an item number in the url, when a user gets to the final page and has the choice to submit the data and finish or add another record, thats where im getting stuck.
 
Last edited:
If you can't put all the fields on one page then sessions would work. Then pre-fill the form values from the session variables.
 
Im not trying to pre-fill the form data, thats easy enough to do if needed.

What I have is a form which collects multiple details about a product (title, value, description etc).

When a user has filled in the information, they can either Submit the data which is then used to generate a CSV file or they can add another item and repeat the process.

I expect people to be using it to create CSV files with hundreds or thousands of items. For one item I have no problems, thats easy, but when they add another Im not sure how to go about handeling the form data.
 
Save each record to a database as it's submitted. At the end read it all back and export to CSV.
 
I agree with Imy, I'd put it in a table in a db, especially if you're talking thousands of rows.....how annoyed would you be if you used something temp like a session var or JS and 1 row from the end something crashed? So it would give the user the ability to leave and return to the work without issue and an easy way to regenerate the csv if needed.

And to answer your other question.

Have 2 'submit' buttons with differing values:

<input type="submit" name="submit" value="submit" />
<input type="submit" name="submit" value="reset" />

and do this in php
Code:
if ($_POST["submit"] == "submit") {
//code for finalising form
} elseif ($_POST["submit"] == "reset") {
// code for reset
} else {
//whatever
}
 
Last edited:
Back
Top Bottom