Re: For your reference, how to validate dynamic form fields.
| From: | holmes072000 at charter dot net | Date: | Wed, 07 Apr 2004 02:11:14 +0000 |
| Subject: | Re: For your reference, how to validate dynamic form fields. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-182587@lists.php.net to get a copy of this message | ||
> From: "Hawkes, Richard" <richard.hawkes@csfb.com>
>
> Just spent far too long trying to figure out how to do this, so I thought I'd
> pass it on to you great guys, in case you need it for the future.
>
> I used PHP to create multiple form rows, each one numbered:
>
> <?
> for ($i =1; $i <= 20; $i++) {
> echo("<input type=text name=userId$i maxlength=5/><br/>");
> }
> ?>
Use arrays. It'll make your life easier.
echo "<input type=\"text\" name=\"userId[$i]\"
maxlength=\"5\"/><br />");
You can still reference the element in Javascript, just "by name" or whatever you call it.
form.element['userId[1]'] (or something like that).
You show a Javascript validation. Remember that this is easily bypassed so you also need to validate
the numbers on the PHP side.
<?php
foreach($_POST['inputId'] as $id)
{
if(!is_numeric($id))
{ echo "$id is invalid!"; }
}
?>
---John Holmes...