Form validation and file structure

I have a form (lets say a registration form) and onsubmit, it calls a function to validate as follows:

$.ajax({
        url : "/ajax/validate.php",
        type : "POST",
        data : $(".form").serialize(),
        success : function(data) {
            data = $.parseJSON(data);
            $.each(data, function(i, item) {
                $('#' + i).addClass('errors');
            });
        }
    });

The validate.php looks something like this:

if ($_POST['email'] == '' || filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == false) {
    $errors['email'] = 'Not a valid email';
}
if ($errors) {
    echo json_encode($errors);
} else {
    // insert to db
}

Now all of this code is specifically for the registration form. I’ll have many forms on my site. My question is, do I make a new file (like validate.php) for each individual form? Such as /ajax/save_profile_data.php etc? Do I keep each form validation separate or is there a clever way to approach this? I don’t know if having a validate file for each form on my site is the correct way to do it.

The general answer would be yes – a separate file for each individual form. If the forms are different and need to be validated a different way, then each file will only need the code for that specific form, otherwise your one file would eventually grow too large in time. Something like /ajax/validations/register.php, /ajax/validations/login.php, /ajax/validations/profile.php, etc.,.

You could get some leverage out of this by having each of the form validation files include other intermediate files that contain re-useable methods to be shared between the different validation pages.

But for most projects I’d recommend using a framework to do as much of the work for you as possible, some of which allow you to create forms with their fields and bind them to a database table through an SQL object mapper, or something else, depending on your requirements.

1

If you have multiple forms submitting to the same page (file), you can use hidden fields in your form to determine which action should take place – to identify the forms by sending along a hidden data field.

For example:

<form id='reg'>
  <input type='hidden' name='act' value='registration'>
  <!-- More HTML --> 
</form>

<form id='news'>
  <input type='hidden' name='act' value='newsletter'>
  <!-- More HTML --> 
</form>

Also, add client side validation to reduce server processing. For this purpose, you can use free jQuery Validation plugin

Download and copy to your website’s directory jquery.validate.js, additional-methods.js and jQuery library itself. In pages where you wish to use validation, reference all these files.

Here is how to use it

HTML

<form action="#" method="post" name="registration">
<input type="text" name="name" required minlength="2" maxlength="30"
            pattern="^[a-zA-Zs]+$" placeholder="Name"
            title="Name is required, and must contain between 2 and 30 alpha characters" />
</form>
<form action="#" method="post" name="newsletter">
<input type="text" name="name" required minlength="2" maxlength="30"
            pattern="^[a-zA-Zs]+$" placeholder="Name"
            title="Name is required, and must contain between 2 and 30 alpha characters" />
</form>

JS

$(function() { 
      $("form").each(function() {
            $(this).validate({
                  submitHandler: function (form) {
                        return false; //prevent actual submit (requirements?)
                  }
            });
      });
});

If we’re talking about a simple web page, your method should be sufficient.

If we’re talking about something of a webapp that is AJAX-heavy, however, it might make sense to redirect all AJAX calls to a single ajax.php. The call should contain the action indicator (e.g. {ajax-action: "validate", form-data: "..."}).

ajax.php then decides what to do based on the action indicator, processes the call and outputs the response. It might be useful, then, to create an ajax_functions.php file which would contain all the functions that might be invoked through AJAX (split it if it becomes too large).

This way, you become more flexible with AJAX calls. For example, you can:

  • attempt to authorize the user before performing the action;

  • efficiently log the AJAX calls;

  • abstract AJAX functionality in Javascript into a single function or class customized to your needs.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật