Tuesday, December 29, 2009

Rash That Feels Like Sunburn

WordPress + jQuery: jQuery

Today we will see an easy way to implement (from scratch
Wordpress theme, which allows users to enter an e-mail to a service
newsletter. Everything will be done with jQuery . What we are going
I developed it to create the new dynamic version of the site of the Xiu Zhen Dao method, which uses WordPress as CMS note: the widget in question is visible in the right sidebar, on any given page

In this case, to make life easier on us and users, there is only one field to be filled with your e-mail, and the widget fails to properly validate this field, such as ensuring that it is this the '@' and at least one point '.' (This will be detailed below):
( "Free Text or HTML). Here is my widget compiled: \u0026lt;div id="newsletter_form"> \u0026lt;fieldset> \u0026lt;input id = "email_news" name = "email_news" type = "text" value = "your e-mail" /> \u0026lt;input id="submit_news" type="submit" value="iscriviti" /> \u0026lt; ; / fieldset> \u0026lt;/ form> \u0026lt;/ div> The CSS used to format the form is almost negligible, because it takes little beyond what is already present in the style of the theme Wordpress, where generally the input box and buttons are already drawn up. This style can be inserted into the document . css main theme, or in any other style sheet included in the page where you will place the widget. / newsletter widget * * / #
newsletter_form # email_news
{width: 130px;} # # newsletter_form submit_news
{margin-top: 5px;
padding: 2px;
cursor: pointer;} # newsletter_form . catched {font-style: italic;}

class
. catched
we will need later in the message of success.
2. jQuery validation
's time to write the script validation: it is essentially identical to what I used in the previous post

, but more time to implement a validation for our field more professional e-mail (as it is the only we have, this time!)
  1. Here's what the script does: when the user clicks the button "subscribe" to check that the field has been filled in correctly. If this does not occur, the field turns red, or else send an email to the address entered and displays a success message.
  2. (function ($) { $ (document). Ready (function () {
  3. / / newsletter widget
$ ('# email_news'). Click (function () { $ (this). Attr ('value','');} )
$ ("# submit_news) . click (function () {
/ / var style error
errorclass
= {'background': '# ff435b', 'color': '# 000', 'borderColor': '# 000'}
/ / analysis of data entered
email_news var = $ ("input # email_news). val ();
var ln = email_news.length;
atpos var = var email_news.indexOf ("@");
 dotpos = email_news . lastIndexOf ("."); 

/ / Field validation e-mail
if ((atpos \u0026lt;1 )
Thanks! You have entered

newsletter
'). Append ('
'.) Hide ();
$ (' # message_news. Catched '). Html (email_news)
$ (' # message_news '). FadeIn (' slow '); }});
return false;}
) 

});
}) (jQuery);

let us look at the first part of the code:


The first statement (lines 5 -7) only serves to automatically clear the text field when the user clicks on it to write its data.

Line 9 catches the event click on the submit button to start the validation procedures.

Lines 12-16 define a CSS class that is used to change the appearance of the input field in error.
The field validation e-mail to check that the entered data meets the following conditions:

must not be null Must contain at least one character '@' and at least one point '.'
The character '@' should not be given at the beginning of the quote
Point '.' must be at least one character away after the '@' but can not stand last.

To do this, first of all, it stores the data entered by the user by the method
 
val ()

, and then examines:


it calculates length by the method



length (line 20)

it locates the '@' character by the method

indexOf ()

(line 21)

you locate the position of the last period '.' inserted by the method

lastIndexOf ()

(line 22).

At this point, you can validate: the conditions are all alone sufficient to make an incorrect e-mail, and are connected by an OR (

if it is in first position, with the control
atpos \u0026lt;1
you see now is if '@' or if there is first

With
dotpos - atpos \u0026lt;2
we made sure that '@' and ' '. not be consecutive, and that the last '.' to the right of '@'

With the latter condition,
dotpos
== ln-1, but if you check the '.' is in last place.
This validation is very similar to that suggested by
w3schools.com

in JavaScript Form Validation .


If the validation does not pass, the field turns red Apply the
. Errorclass
(line 26).

Everything that comes after concerns the transfer of data to a php script for sending e-mail, and has been addressed in detail in the post
form validation with jQuery
.
In essence, we use the method

$. Ajax
, which also allows us to display a success message, without having to reload this or other pages.
Noteworthy, this time, the inclusion of data stored in the message of success to you, confirming what he had inserted just insert a
    \u0026lt;span>
  1. with a class for it (in my case
  2. . catched
  3. ) and pass the stored value, by the method
  4. html (val)
(line 40).
  1. 3. PHP script
  2. The last thing you see is the script that sends e-mail, and now there's really
  3. minimal! I have written like this:
  4. \u0026lt;?
php $ body = "e-mail writing". $ _POST ['Email_news']; $ header = "From:". $ _POST ['Email_news' ] mail ("indirizzo@email.dominio", "widget Newsletter", $ body, $ header); ?>
  1. 4. files to include
  2. summary, after inserting the HTML code widget (1 above), you must enter a jQuery script file that we have seen above (Section 2) in the header.php template (and at least in the \u0026lt;head> of each page that will display the widget), for example like this:
  3. \u0026lt;script type="text/javascript" src="newsletter_validation.js"> \u0026lt;/ script> Then you have to put the php script at the address specified in the url of the method $.
  4. ajax (perhaps the most convenient place is an absolute URL after placing the file, for example in the theme directory Wordpress).

Sunday, December 6, 2009

How Can I Oil My Clippers

widget Newsletter: custom form validation

I could have titled this post "principles of form validation," because what we will see how it is possible to write a simple form validation script from scratch.
You can use the popular jQuery plugin called just
    Validation
  1. created by Jörn Zaeferrer , which adds in a very simple custom function to validate a form. We will not use this road here, but look closely at what the problems and needs which must be faced if we want to validate a form with jQuery, and you'll get the flexibility that results from a greater understanding. Our goal is to have a final form that is valid without the need to load another page and send an email formatted arbitrarily, and that while it is flexible enough to support changes to the general recipe. The example comes from the contact form that I have implemented the "sleek" Site www.bfelettrotecnica.it
  2. , and the base from which shall be equal to that proposed in this tutorial
  3. of
  4. Net Tuts + ( to the work of Eric
  5. ), and that the Board of reading, though, before you begin.

1. HTML markup
As you can see from the picture, the form contains many fields, many mandatory (but not all), a select, textarea, a radio and even two for a unique selection: allows the user to choose whether to represent an individual or a company, and fill out the fields that vary According to this choice.
Here is the code of the form:
\u0026lt;div id="contactform">
\u0026lt;form action="#" method="post"> \u0026lt;fieldset> \u0026lt;legend> Request Information \u0026lt; / legend> \u0026lt;ol> \u0026lt;li>
\u0026lt;input id = "private" type = "radio" value = "private" name = "type" /> \u0026lt;span class="radioin"> Private \u0026lt;/ span> /> \u0026lt;label for="nome"> Name * \u0026lt;i> \u0026lt; / i> \u0026lt;/ label> \u0026lt;input id="nome" name="nome" type="text" class="obb" /> \u0026lt;label id='nome_err' class='error'> ; enter your name \u0026lt;/ label> \u0026lt;/ li> \u0026lt;li> \u0026lt;input id = "company" type = "radio" value = "company" name = "type" /> \u0026lt;span class="radioin"> Company \u0026lt;/ span> /> \u0026lt;label for="azienda"> \u0026lt;i> Company Name * \u0026lt;/ i> \u0026lt;/ label> \u0026lt;input
id="azienda" name="azienda" type="text" class="obb" />
\u0026lt;label id='azienda_err' class='error'> Enter the name \u0026lt;/ label> \u0026lt;label for="referente"> Contact \u0026lt;i> * \u0026lt;/ i> \u0026lt;/ label> \u0026lt;input id="referente" name="referente" type="text" class="obb" />
\u0026lt;label id='referente_err' class='error'> enter the name of a contact \u0026lt; / label> \u0026lt;/ li>
\u0026lt;li style="margin-top: 15px;">
 \u0026lt;label for="indirizzo"> \u0026lt;i> Address * \u0026lt;/ i> \u0026lt; / label> \u0026lt;input 
id="indirizzo" name="indirizzo" type="text" class="obb"
/> \u0026lt;label id = 'indirizzo_err' class = 'error'> enter the address of \u0026lt;/ label>
\u0026lt;label style="width: 75px;" for="cap"> CAP \u0026lt;i> * \u0026lt; , / i> \u0026lt;/ label> \u0026lt;input
style="width: 50px" id="cap" name="cap" type="text" class="obb"
/> \u0026lt;label id = ' cap_err 'class =' \u200b\u200berror '> enter the zip code \u0026lt;/ label>
\u0026lt;/ li>
\u0026lt;li> \u0026lt;label for="citta"> City \u0026lt;i> * \u0026lt;/ i> \u0026lt;/ label> \u0026lt;input
id="citta" name="citta" type="text" class="obb"
/> \u0026lt;label id = 'citta_err 'class =' \u200b\u200berror '> type the name of the city \u0026lt;/ label>
\u0026lt;label style="width: 75px;" for="provincia"> \u0026lt;i> Province * \u0026lt; / i> \u0026lt;/ label> \u0026lt;input style="width: 50px;" id="provincia" name="provincia" type="text" class="obb" /> \u0026lt;label id='provincia_err' class='error'> enter the province code \u0026lt;/ label>
\u0026lt;/ li> \u0026lt;li> \u0026lt;label for="telefono"> Phone \u0026lt;/ label> \u0026lt;input id="telefono" name="telefono"
type="text" /> \u0026lt;/ li>
 
\u0026lt;li>
\u0026lt;label for="email"> e-mail \u0026lt;i> * \u0026lt;/ i> \u0026lt;/ label>
\u0026lt;input id = "email" name = "email" type = "text" class = "OBB" /> \u0026lt;label id='email_err' class='error'> enter your e-mail \u0026lt;/ label> \u0026lt;/ li> \u0026lt;label for="area"> Area of \u200b\u200bInterest \u0026lt;/ label>
\u0026lt;select id="area" name="area"> \u0026lt;option value="void"> \u0026lt;/ option> \u0026lt;option value="centraline"> Control Unit \u0026lt;/ option> \u0026lt;option value="plc"> Programming PLC \u0026lt;/ option> \u0026lt;option value="Automazione"> Industrial Automation \u0026lt;/ option> \u0026lt;option
value="Quadri Elettrici"> Switchboards \u0026lt;/ option>
\u0026lt;option value = "Electrical Systems" > Electrical \u0026lt;/ option>
\u0026lt;/ select>
\u0026lt;/ li>
\u0026lt;li> \u0026lt;label for="messaggio"> Message: \u0026lt;i> * \u0026lt;/ i> \u0026lt; ; / label> \u0026lt;textarea id = "post" name = "message" rows = "5" cols = "38" class = "OBB"> \u0026lt;/ textarea> \u0026lt;label id='messaggio_err' class='error'> write the Your request \u0026lt;/ label> \u0026lt;/ li> \u0026lt;/ ol> \u0026lt;/ fieldset> \u0026lt;span id="input"> \u0026lt;input id = "submit" type = "submit" value = "Send Message" /> \u0026lt;/ span> \u0026lt;span style="font-style: italic;">
Personal data will be processed in accordance with privacy laws. \u0026lt;/ span>
\u0026lt;/ form>
\u0026lt;/ div> points worthy of note are:


the first line, action = "#" , because we will use jQuery to handle all of the part relating to the transfer of data php script that will send the final email.

tags
\u0026lt;label>
from class
 error are error messages that appear if the respective required field has not been completed. They are hidden by default. 

Required fields are denoted as usual with an asterisk (*), contained in a tag italics
\u0026lt;i>
, And class

OBB.

Tag
\u0026lt;div>
that has all the form will be required when viewing a success message to the user.


class "error" is dedicated to error messages, while the class "errorinput" is incorrectly filled fields. Need something that catches your eye, and in these cases the color of choice is red. I used something like this, passed through the CSS of the page:


.
error {color: red;
font-style: italic;
float: left;}

. Errorinput
{border: 1px solid red ;
background: # ffcbcb;
color: # 000;
margin-bottom: .5 em;}




property
float
I need the rows with two input tags, not to destroy the layout of the form.



2. jQuery: Form validation


2.0 basic validation


The first thing to do is hide the error messages and take possession of the event click on the submit button on the form, to perform our validation, instead of sending just the facts:


$ (document). ready (function () {$

(. 'error'). hide ();

$ ("# submit"). click (function () {

/ / here we go all the validation that is activated when the user wishes to send the message



}});


So far we are almost verbatim the tutorial
Net Tuts +
.

The validation is now expected to check each input field, and if it is empty, view the error message.

Net Tuts +
has only three fields, and then writes the validation function three times, once for each field: as I have

a lot of fields, I speed things up with a beautiful cycle.

First, you initialize a variable,
datastring
, which will contain all data to pass to the php mail script, and will be in the form:

datastring = [nome_campo_1 =] + [valore_campo_1] + [& nome_campo_2 =] + [valore_campo_2] + ... + [& Nome_campo_n =] + [valore_campo_n]
As per specifications of the GET method of php.
->
    Then, to analyze each element
  • \u0026lt;input> iterative method is used
  • each (callback)
  • : \u0026lt;input> for each tag (other than the radio button ' sending one or two of the top radio, and is not a disabled field, as discussed in paragraph 4 below) and for the textarea script must save two pieces of information: the value (with the data entered by the user), obtained by the method val ()
  • , and his ID. With this we now create the ID of the corresponding error message by adding the suffix "_err.
  • Here is the script for these first steps: $ ("# submit"). Click (function () {$
  • (. 'Error'). Hide (); datastring var = "";
$ ('input [id! = submit] [type! = radio], textarea, select'). not ('input [disabled]'). each (function (i)
{var val = $ (this). val ();
var id = $ (this). attr ("id");
var err = id + "_err"
});} 





I quote the statement that hides the error messages here to reset the validation each time you press the submit button.


Now, the validation in itself: if the field is empty (
val == ""
) and the field is required (ie, holding the class

OBB), you must do two actions: Assign error class "errorinput" (seen above) to tag \u0026lt;input>
to color it red,
view the error message, just manipulated by the ID.
If validation passes, it stores the data entered by appending the variable datastring
.

entering these operations, the cycle becomes complete as follows:
 
$ ("# submit"). Click (function () {var

datastring = "";

$ ('input [id! = Submit] [type! = radio], textarea, select '). not (' input [disabled] '). each (function (i)
{var val = $ (this). val ();
var id = $ (this .) attr ("id");
var err = id + "_err"

/ / error notification to the required fields
if ((val == "") & & ($ (this). is ( '. OBB')))
{$ (this). AddClass ('errorinput'). Focus ();
$ ("label [id =" + err + "]"). slideDown (); return true;} else {
$ ( this). removeClass ('errorinput'), / / \u200b\u200bremoval of protective class error

/ / storing data assessed if (i == 0) { datastring = id + '=' + val;} else { datastring datastring + = '&' + id + '=' + val;}

}});
 
}});