Solved

best way to dynamically create jquery validation for a form created dynamically in php

Posted on 2014-03-04
1
888 Views
Last Modified: 2014-03-07
Hello

I am writing an ajax website with forms created dynamically in php and the validation done dynamically in jquery from the php.

This can be quote tedious with a lot of concatentation and potential for errors. I was wondering if you experienced web developers had a good way of making your jquery validation dynamically.

I saw an idea on this post
http://stackoverflow.com/questions/21439639/building-dynamic-rules-for-jquery-validate-from-php-array

where the jquery is created in javascript from json but to be honest I would rather create it on the server as i dont know php/query/json very well as it is. What i'm doing isnt my usual job.

But given as this must be a common task I am assuming there is a framework/approach for doing this.

I did look at formbuilders and they dont seem too popular

Thanks
0
Comment
Question by:andieje
1 Comment
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39903355
Unless you choose a framework, there is no magic bullet; you just have to do the work.  And if you choose a framework, then you have to learn the framework in addition to learning PHP and jQuery, so that is probably a longer path than doing it by hand.

Make a Google search for the exact phrase "jquery validation example" to find examples.  Although it does no client-side validation, this article shows the basics of jQuery and AJAX.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html

You may want to learn about PHP HEREDOC syntax.  I find it very easy and powerful for making templates and HTML fragments.  An example is in the code snippet.  You can think of HEREDOC as notation that is like a double-quoted string.  It has variable substitution and if you use it with object-oriented notation, you don't have to escape the quote marks, so it reduces syntax issues and parse errors.

<?php // html5_template.php
error_reporting(E_ALL);

// CREATE VARIABLES FOR OUR HTML
$dat = date('r');
$xyz = "Hello World.  It is $dat";

// CREATE OUR WEB PAGE IN HTML5 FORMAT
$htm = <<<HTML5
<!DOCTYPE html>
<html dir="ltr" lang="en-US">

<head>

<meta charset="utf-8" />
<meta name="robots" content="noindex, nofollow" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

<title>HTML5 Page in UTF-8 Encoding</title>

</head>

<body>

<p>$xyz</p>

</body>

</html>
HTML5;

// RENDER THE WEB PAGE
echo $htm;

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question