Solved

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

Posted on 2014-03-04
1
884 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 109

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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

828 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