Forum Moderators: open
A snippet of code:
function Verify_Event(eventForm){
var msg="Processing your form produced the following errors:\n";
var errors=0;
for (i=1;i<=eventForm.att.value;i++) {
fnameA="fname"+i;
lnameA="lname"+i;
fname=eventForm.fnameA;
lname=eventForm.lnameA;
if (!fname.value ¦¦!lname.value) {
msg+="\n- Attendee #"+i+" - full name is required";
errors++;
}
}
if (errors!= 0) {
alert(msg);
return false;
}
}
You would call this function as such:
<input... onClick="return Verify_Event(this.form)">
So the problem is grabbing all of those dynamic values in a FOR loop. I'm not very stellar with JavaScript, so any suggestions will help.
firstName=eval("eventForm.fname"+i);
lastName=eval("eventForm.lname"+i);
if (!firstName.value ¦¦!lastName.value) {
msg+="\n- Attendee #"+i+" - full name is required";
errors++;
}
<?php
// presuming number to be added passed as variable $num//start form
echo <<<OUT
<form action="process.php" method="post" onsubmit="validate('$num')" id="myform">
OUT;$i=0;
while ($i < $num){
echo <<<ROW
Enter Person $num:
firstname:<br />
<input type="text" name="fname[]" />
lastname:<br />
<input type="text" name="lname[]" />
...
...
ROW;
$i++;
}
echo <<<END
<inpu type="submit" name="submit" value="Add People" />
</form>END;
?>
then, to process the values, you can simply loop again with PHP:
<?php$total=count($_POST['fname']);
$i=0
while ($i <=$total){$this_fname=$_POST['fname'][$i];
$this_lname=$_POST['lname'][$i];// add to DB as required
$i++
}
?>
Just an option to reduce the work involved in counting them all and sending additional data through the form
HIH