Forum Moderators: coopster
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
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.
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";
}
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.
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).
>> 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.
<?
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.. ;)