Forum Moderators: coopster

Message Too Old, No Replies

Submitting multiple rows with one form.

         

dkin

10:10 pm on Sep 9, 2004 (gmt 0)

10+ Year Member



I am trying to create a form for my users to submit multiple rows to a databse, I have a few questions.

1. I would like the form to have an option before they start filling out the form, the option should be how many kinds of pop they would like to submit with options 1-5.

2. I would like the information that they submit to be submitted to the database but, I would like it to be in different rows. eg 5 kinds of pop 5 rows.

If someone could give me a few hints I would be grateful, thank you.

Cheers

mincklerstraat

8:07 am on Sep 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll probably want to do some research for this project, which could easily involve reading the tutorial at php.net, and maybe a tutorial for the type of database concerned. After this more basic research, you will probably want to read up some more on arrays, and on control structures - the control structures 'while' and 'foreach' come to mind.

Zipper

8:34 am on Sep 10, 2004 (gmt 0)

10+ Year Member



ok.. just need to get things a bit clearer..
are you trying to do something like this..

1 - user chooses N number of pop to be submitted (Where N is between 1-5).
2 - the form is generated with N number of input elements.
3 - once the form is submitted, the records are stored in N number of rows.

If yes, please post the form elements required for a single pop, as it'll be easier to figure out a solution.

SkyDog

3:20 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



Just put the fields in the form as arrays. Unlike perl, php doesn't recognize arrays in forms unless the variable name is explictly defined as an array. You do this by adding a "[]" after the variable name. This makes for some javascript nightmares but php requires it. Here is an example:

The form ->
Enter Pop 1<input type="text" name="pop[]">
Enter Pop 2<input type="text" name="pop[]">
Enter Pop 3<input type="text" name="pop[]">
The Code to Process the form ->
for($xx=0;$xx<count($pop);$xx++){
//filter user input example allows only alpanumeric and space
$pop[$xx]=preg_replace("/[^0-9a-z\s]/i","",$pop[$xx]);
//if they only entered say 2 out of 3 maximum rows
if(!$pop[$xx])break;
echo "pop[$xx]<br>\n";
}

Zipper

4:22 pm on Sep 10, 2004 (gmt 0)

10+ Year Member



I wouldn't jump into that conclusion because there is no clear indication as to how many form elements are required for a single pop. Besides there is no need for such complex issues concerning server/client side compatibility (referring to javascript).

If the user is given the option to 'select' how many pops, then of course the script has the actual figure for the number of pops, which eliminates the need of arrays. I wouldn't want to comment further on this until the thread starter provides us with more info.

SkyDog

5:02 pm on Sep 10, 2004 (gmt 0)

10+ Year Member




I wouldn't jump into that conclusion because there is no clear indication as to how many form elements are required for a single pop.

Its rather trivial to add the corresponing elements for a single pop. They would all be at the same ordinal position in their respective arrays.

Besides there is no need for such complex issues concerning server/client side compatibility (referring to javascript).

Not for this application, but in general it can be an issue.

dkin

12:33 am on Sep 11, 2004 (gmt 0)

10+ Year Member



there will be roughly 5 input elements per "N".

dkin

8:00 pm on Sep 11, 2004 (gmt 0)

10+ Year Member



Zipper if you would, could you please explain what you were asking about, thank you very much

Cheers

Zipper

9:12 pm on Sep 11, 2004 (gmt 0)

10+ Year Member



I partly agree with SkyDog, but would like to stick to stanadard practices in web development (php).

>> there will be roughly 5 input elements per "N".
>> could you please explain what you were asking about

Let's try a run thru,

1 - Get the value of N (the number of pops the user needs to submit) thru a form. (let's say N = 3)
2 - Using this value (eg: $_POST['npop']) you print the form elements for the user to input the actual pops. That's if N is 3, then you run a routine to print the "5 input elements" thrice.
3 - When the user inputs the N pops and submits the form you start inserting them to N number of rows each containing 5 fields for the "5 input elements".

If it's clear we can move on to the coding.

dkin

11:11 pm on Sep 11, 2004 (gmt 0)

10+ Year Member



that is clear. i understand what we are trying to do now, the next part is where i am baffled. lol.

Cheers

Zipper

1:58 am on Sep 12, 2004 (gmt 0)

10+ Year Member



This is a one page senario.. and is in the simplest form (no arrays). I'm unaware of the exact types of the "5 input elements" you mentioned, however just take look at it generally and try to get the idea.

<?
if(!isset($_POST['npop'])){
// this is where the user will select how many pops to insert
?>
<form method="post">
<select name="npop">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
<input type="submit" value="Continue">
</form>
<?
} else {
if(!isset($_POST['pop'])){
$pop = $_POST['npop'];
// when the user has selected the N number of pops
// its time to create the N input elements
?>
<form method="post">
<input type="hidden" name="npop" value="<?=$pop;?>">
<input type="hidden" name="pop" value="1">
<?
for($i = 1;$i <= $pop; $i++){
echo "<h3>Pop $i</h3>";

// now comes the 5 input elements..
// add them as u like naming them in the following pattern,
// or whatever prefix
?>
Element 1 : <input type="text" name="a<?=$i;?>" value=""><br />
Element 2 : <input type="text" name="b<?=$i;?>" value=""><br />
Element 3 : <textarea name="c<?=$i;?>"></textarea><br />
Element 4 : <select name="d<?=$i;?>">
<option value="1">Something</option>
<option value="2">Something</option>
</select><br />
Element 5 : <input type="radio" name"e<?=$i;?>" value="1" checked>Something
<input type="radio" name"e<?=$i;?>" value="2">Something <br />
<?
}
?>
<input type="Submit" value="Submit Form">
</form>
<?
} else {
$pop = $_POST['npop'];

// when the user has submitted the pops,
// they will be inserted to N rows in the table.

@mysql_connect("localhost", "username", "password");
@mysql_select_db("dbname");

for($i = 1; $i <= $pop; $i++){
$a = $_POST['a'.$i]; $b = $_POST['b'.$i]; $c = $_POST['c'.$i];
$d = $_POST['d'.$i]; $e = $_POST['e'.$i];
$sql = "INSERT INTO table VALUES('$a', '$b', '$c', '$d', '$e')";
mysql_query($sql) or die(mysql_error());
}

echo "POPs were recorded. Thank you";

}
}
?>

make your modifications... I'm sure it won't work otherwise.. sorry abt the messy coding, my head's spinning right now.. ;)

dkin

6:30 am on Sep 12, 2004 (gmt 0)

10+ Year Member



I have taken a look at the code and from what I try to understand...... I dont lol.

I will try to take a deeper look at the code in the morning, but right now I need some sleep :D thank you for your contribution.

Cheers