bphein1980
07-16-2010, 06:29 PM
How can the javascript be shortened? I always try to make code as short and efficient as possible. Can any shortcuts be applied here? Maybe there is a better way to wrap this up in a function to make it more usable?
Basics of what it is doing:
When the form button is clicked: showing loading span and making ajax call
When ajax is success, hide loading span and show response message from XML
Parsing XML response and adding error class on input if any errors
Response message can be clicked to close
<html>
<head>
<style type="text/css">
form {
margin:0;
padding:0;
}
label {
display:block;
float:left;
width:100px;
text-align:right;
padding-right:5px;
margin-bottom:3px;
}
input {
margin-bottom:3px;
padding:3px;
border:1px solid #aaa;
}
a {
border:1px solid #999;
background:#bbb;
color:#eee;
text-decoration:none;
padding:0 5px;
margin:3px 5px 0 0;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
}
fieldset {
width:270px;
padding:10px;
}
.error {
border:1px solid #cc6666;
background:#ffcccc;
}
#ajaxResponse {
background:#ffffcc;
border:1px solid #ffcc99;
padding:10px;
margin-bottom:5px;
display:block;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
cursor:pointer;
cursor:hand;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// clears response span when span is clicked
$("#ajaxResponse").click(function(){
$(this).fadeOut("slow");
});
// hide stuff by default
$("#ajaxLoading").hide();
$("#ajaxResponse").hide();
// click trigger
$("#trigger").click(function(){
// show loading span
$("#ajaxLoading").show();
// set POST vars
var ifirstname = $("[name=firstname]").val();
var ilastname = $("[name=lastname]").val();
var icompany = $("[name=company]").val();
// start ajax
$.ajax({
url: 'process.cfm',
type: 'POST',
data: 'signup=1&firstname='+ifirstname+'&lastname='+ilastname+'&company='+icompany,
dataType: 'xml',
success: function(xml){
// clear any errors
$("input").removeClass("error");
// re-hide stuff
$("#ajaxLoading").hide();
$("#ajaxResponse").hide();
// find which errors are present and outline input
$(xml).find("error").each(function(){
var iName = $(this).text();
$("[name="+iName+"]").addClass("error");
});
// display response message
var res = $(xml).find("message").text();
$("#ajaxResponse").html(res);
$("#ajaxResponse").fadeIn('slow');
}
});
});
});
</script>
</head>
<body>
<form action="" method="POST" name="myForm">
<fieldset>
<legend>Contact Us</legend>
<span id="ajaxResponse"></span>
<label>First Name</label>
<input type="text" name="firstname" value="Tester"/><br/>
<label>Last Name</label>
<input type="text" name="lastname" value="Testing"/><br/>
<label>Company</label>
<input type="text" name="company" value="XYZ Company"/><br/>
<label> </label>
<a href="#" id="trigger">Submit</a> <span id="ajaxLoading">Loading...</span>
</fieldset>
</form>
</body>
</html>
The process.cfm page:
<cfset error = 0>
<cfxml variable="ajaxResponse">
<response>
<errors>
<cfoutput>
<cfif LEN(#FORM.firstname#) LT 1>
<cfset error = 1>
<error>firstname</error>
</cfif>
<cfif LEN(#FORM.lastname#) LT 1>
<cfset error = 1>
<error>lastname</error>
</cfif>
<cfif LEN(#FORM.company#) LT 1>
<cfset error = 1>
<error>company</error>
</cfif>
</cfoutput>
</errors>
<message>
<cfoutput>
<cfif error GT 0>
Please correct the fields in red
<cfelse>
Success!
</cfif>
</cfoutput>
</message>
</response>
</cfxml>
<cfset xmlString = ToString(ajaxResponse)>
<cfcontent type="text/xml" reset="yes">
<cfoutput>
#xmlString#
</cfoutput>
Basics of what it is doing:
When the form button is clicked: showing loading span and making ajax call
When ajax is success, hide loading span and show response message from XML
Parsing XML response and adding error class on input if any errors
Response message can be clicked to close
<html>
<head>
<style type="text/css">
form {
margin:0;
padding:0;
}
label {
display:block;
float:left;
width:100px;
text-align:right;
padding-right:5px;
margin-bottom:3px;
}
input {
margin-bottom:3px;
padding:3px;
border:1px solid #aaa;
}
a {
border:1px solid #999;
background:#bbb;
color:#eee;
text-decoration:none;
padding:0 5px;
margin:3px 5px 0 0;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
}
fieldset {
width:270px;
padding:10px;
}
.error {
border:1px solid #cc6666;
background:#ffcccc;
}
#ajaxResponse {
background:#ffffcc;
border:1px solid #ffcc99;
padding:10px;
margin-bottom:5px;
display:block;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
cursor:pointer;
cursor:hand;
}
</style>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
// clears response span when span is clicked
$("#ajaxResponse").click(function(){
$(this).fadeOut("slow");
});
// hide stuff by default
$("#ajaxLoading").hide();
$("#ajaxResponse").hide();
// click trigger
$("#trigger").click(function(){
// show loading span
$("#ajaxLoading").show();
// set POST vars
var ifirstname = $("[name=firstname]").val();
var ilastname = $("[name=lastname]").val();
var icompany = $("[name=company]").val();
// start ajax
$.ajax({
url: 'process.cfm',
type: 'POST',
data: 'signup=1&firstname='+ifirstname+'&lastname='+ilastname+'&company='+icompany,
dataType: 'xml',
success: function(xml){
// clear any errors
$("input").removeClass("error");
// re-hide stuff
$("#ajaxLoading").hide();
$("#ajaxResponse").hide();
// find which errors are present and outline input
$(xml).find("error").each(function(){
var iName = $(this).text();
$("[name="+iName+"]").addClass("error");
});
// display response message
var res = $(xml).find("message").text();
$("#ajaxResponse").html(res);
$("#ajaxResponse").fadeIn('slow');
}
});
});
});
</script>
</head>
<body>
<form action="" method="POST" name="myForm">
<fieldset>
<legend>Contact Us</legend>
<span id="ajaxResponse"></span>
<label>First Name</label>
<input type="text" name="firstname" value="Tester"/><br/>
<label>Last Name</label>
<input type="text" name="lastname" value="Testing"/><br/>
<label>Company</label>
<input type="text" name="company" value="XYZ Company"/><br/>
<label> </label>
<a href="#" id="trigger">Submit</a> <span id="ajaxLoading">Loading...</span>
</fieldset>
</form>
</body>
</html>
The process.cfm page:
<cfset error = 0>
<cfxml variable="ajaxResponse">
<response>
<errors>
<cfoutput>
<cfif LEN(#FORM.firstname#) LT 1>
<cfset error = 1>
<error>firstname</error>
</cfif>
<cfif LEN(#FORM.lastname#) LT 1>
<cfset error = 1>
<error>lastname</error>
</cfif>
<cfif LEN(#FORM.company#) LT 1>
<cfset error = 1>
<error>company</error>
</cfif>
</cfoutput>
</errors>
<message>
<cfoutput>
<cfif error GT 0>
Please correct the fields in red
<cfelse>
Success!
</cfif>
</cfoutput>
</message>
</response>
</cfxml>
<cfset xmlString = ToString(ajaxResponse)>
<cfcontent type="text/xml" reset="yes">
<cfoutput>
#xmlString#
</cfoutput>