Re: iterate javascript verification

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I took your code and modified it to use HTML5 validation (and few other changes). You can see the results at <http://my-testbed.com/test1/form_validation.php>http://my-testbed.com/test1/form_validation.php

My code follows:

  <?php
$fields = array('first_name','last_name','department','title','email','phone');
  $num_forms = 1;
  $tmp = array();
  $errors = array();

   if (isset($_POST['submit'])) {
    $requestor_email = $_POST['requestor_email'];
    $num_forms  = $_POST['num_forms'];
    for ($i = 1;$i <= $num_forms; ++$i) {
        foreach ($fields as $fld) {
                if ($_POST[$fld][$i] == '') {
$errors[] = ucwords(str_replace('_',' ',$fld)) . " for account $i can not be blank";
                }
        }
    }
  }
                if (!empty($errors)) {
                        $tmp[] = "The following fields are in error:<br>";
                        $tmp[] = implode("<br>\n",$errors);
                        $tmp[] = "<br>";
                }
$tmp[] = "<div style='text-align:center'>You will be creating $num_forms accounts today.</div><br>"; $tmp[] = '<div style="text-align:center"><form name="ldap_accounts" method="post" action="">';
    $tmp[] = '<br /><br />';
    for($counter = 1;$counter<=$num_forms;$counter++) {
        $tmp[] = "Enter user: $counter<br />";
        $tmp[] = "<label for='first_name_$counter'>First Name:</label><br/>";
$tmp[] = "<input type='text' required id='first_name_$counter' name='first_name[$counter]'><br /><br />";
        $tmp[] = "<label for='last_name_$counter'>Last Name:</label><br />";
$tmp[] = "<input type='text' required id='last_name_$counter' name='last_name[$counter]' /><br /><br />";
        $tmp[] = "<label for='department_$counter'>Department:</label><br/>";
$tmp[] = "<input type='text' required id='department_$counter.' name='department[$counter]' /><br /><br />";
        $tmp[] = "<label for='title_$counter'>Title:</label><br />";
$tmp[] = "<input type='text' required id'title_.$counter' name='title[$counter]' /><br /><br />";
        $tmp[] = "<label for='email_.$counter'>Email:</label><br />";
$tmp[] = "<input type='email' required id='email_.$counter' name='email[$counter]' /><br /><br />";
        $tmp[] = "<label for='phone_$counter'>Phone:</label><br />";
$tmp[] = "<input type='text' required id='phone_$counter' name='phone[$counter]' /><br /><br />";
  }
$tmp[] = "<input type='hidden' id='num_forms' name='num_forms' value='$num_forms' /><br /><br />"; $tmp[] = "<input type='hidden' id='requestor_email' name='requestor_email' value='$requestor_email' />";
  $tmp[] = "<input type='submit' name='submit' value='Create Ticket' />";
  $tmp[] = "</form></div>";

?>
<!DOCTYPE html>
<html>
<head>
<title>LDAP Form</title>
<body>
        <?php echo implode("\n",$tmp) . "\n"; ?>
</body>
</html>

You will notice that I moved the code for the form to above the HTML section. I believe that very little PHP should be interspersed with the HTML -- it makes for cleaner code. You can use single quotes around form attributes so you don't have to escape the double quotes. The names in the form are now arrays. This makes your life much easier when extracting the values later in PHP.

When you check the page in a HTML5 aware brower, you will see how the validation is done.

Ken

At 10:17 PM 5/24/2013, musicdev wrote:
You can validate via JS if required, for example: (JS CODE):

if(element.value.length == 0){
   // handle 0 length value
}

I do agree with Ken that you SHOULD NOT perform JS validation.  It is
preferable to use php or the new HTML5 features.  JS can be turned-off by
the user which will make JS validation impossible.


On Fri, May 24, 2013 at 8:07 PM, Tim Dunphy <bluethundr@xxxxxxxxx> wrote:

> Hello list,
>
>  I have a php script that creates a variable number of forms based on a
> $_POST variable from a preceding page. It then takes the data input into
> the form and neatly packages the result into an email sent to an email
> address (eventually to be a ticketing system).
>
>
> Almost everything on the page works great. The only thing I can't seem to
> get working is how to verify that the fields in the form are not left empty
> using javascript. The syntax I'm using seems like it should work, however
> when I leave one or more of the fields empty, the email gets sent anyway
> with the missing data.
>
> Here's the app I was hoping someone might be able to suggest a successful
> approach:
>
> <html>
> <head>
> <title>LDAP Form</title>
> <body>
>   <?php
>
>    if (isset($_POST['submit'])) {
>     $requestor_email = $_POST['requestor_email'];
>     $num_forms  = $_POST['num_forms'];
>     }
>
>     echo "<center>You will be creating $num_forms accounts
> today.</center><br />";
>     for($counter = 1;$counter<=$num_forms;$counter++) {
>         echo '<center><form name="ldap_accounts" method="post"
> action="sendemail.php" onsubmit="return validateForm()">';
>         echo '<br /><br />';
>         echo "Enter user: $counter<br />";
>         echo "<label for=\"first_name_.$counter.\">First Name:</label><br
> />";
>         echo "<input type=\"text\" id=\"first_name_.$counter.\"
> name=\"first_name_.$counter.\" /><br /><br />";
>         echo "<label for=\"last_name_.$counter.\">Last Name:</label><br
> />";
>         echo "<input type=\"text\" id=\"last_name_.$counter.\"
> name=\"last_name_.$counter.\" /><br /><br />";
>         echo "<label for=\"department_.$counter.\">Department:</label><br
> />";
>         echo "<input type=\"text\" id=\"department_.$counter.\"
> name=\"department_.$counter.\" /><br /><br />";
>         echo "<label for=\"title_.$counter.\">Title:</label><br />";
>         echo "<input type=\"text\" id=\"title_.$counter.\"
> name=\"title_.$counter.\" /><br /><br />";
>         echo "<label for=\"email_.$counter.\">Email:</label><br />";
>         echo "<input type=\"text\" id=\"email_.$counter.\"
> name=\"email_.$counter.\" /><br /><br />";
>         echo "<label for=\"phone_$counter.\">Phone:</label><br />";
>         echo "<input type=\"text\" id=\"phone_.$counter.\"
> name=\"phone_.$counter.\" /><br /><br />";
>   ?>
>    <script>
>     function validateForm()
>      {
>        var a=document.forms["ldap_accounts"]["first_name_"].value;
>        if (a==null || a=="")
>        {
>         alert("User $counter first name must be filled out.");
>         return false;
>        }
>         var b=document.forms["ldap_accounts"]["last_name_"].value;
>         if (b==null || b=="")
>         {
>         alert("User $counter last name must be filled out.");
>         return false;
>         }
>         var c=document.forms["ldap_accounts"]["department_"].value;
>         if (c==null || c=="")
>         {
>         alert("User $counter department must be filled out.");
>         return false;
>         }
>         var d=document.forms["ldap_accounts"]["title_"].value;
>         if (d==null || d=="")
>         {
>         alert("User $counter title must be filled out.");
>         return false;
>         }
>         var d=document.forms["ldap_accounts"]["email_"].value;
>         if (d==null || d=="")
>         {
>         alert("User $counter address must be filled out.");
>         return false;
>         }
>         var d=document.forms["ldap_accounts"]["phone_"].value;
>         if (d==null || d=="")
>         {
>         alert("User $counter phone name must be filled out.");
>         return false;
>         }
>       }
>      </script>
>  <?php
>   }
>
>   echo "<input type=\"hidden\" id=\"num_forms\" name=\"num_forms\"
> value=\"$num_forms\" /><br /><br />";
>   echo "<input type=\"hidden\" id=\"requestor_email\"
> name=\"requestor_email\" value=\"$requestor_email\" />";
>   echo "<input type=\"submit\" name=\"submit\" value=\"Create Ticket\" />";
>   echo "</form></center>";
>
>    ?>
> </body>
> </html>
>
> Thanks,
> Tim
>
> --
> GPG me!!
>
> gpg --keyserver pool.sks-keyservers.net --recv-keys F186197B
>

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux