Skip to content

Commit

Permalink
Add a client-side form example
Browse files Browse the repository at this point in the history
  • Loading branch information
reyiyo committed Nov 13, 2016
1 parent 5ffce2c commit 354b05c
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
5 changes: 5 additions & 0 deletions examples/jquery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# JQuery Example

This is a very simple client-side form that submits the contact form with ajax to the webtask server.

To try it, make sure you change the form's action parameter in the HTML with the url of yout webtask.
123 changes: 123 additions & 0 deletions examples/jquery/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<html>
<head>
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/sweetalert2/6.1.0/sweetalert2.min.css">
</head>
<body>
<div class="container">
<form class="well form-horizontal" action="<your webtask url>" id="contact-form">
<fieldset>

<legend>Contact Us Today!</legend>

<div class="form-group">
<label class="col-md-4 control-label">Name</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-user"></i>
</span>
<input name="name" placeholder="Name" class="form-control" type="text" required>
</div>
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">E-Mail</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-envelope"></i>
</span>
<input name="email" placeholder="E-Mail Address" class="form-control" type="text" required>
</div>
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Phone #</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-earphone"></i>
</span>
<input name="phone" placeholder="(845)555-1212" class="form-control" type="text">
</div>
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Company</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-home"></i>
</span>
<input name="company" placeholder="Company" class="form-control" type="text">
</div>
</div>
</div>

<div class="form-group">
<label class="col-md-4 control-label">Message</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon">
<i class="glyphicon glyphicon-pencil"></i>
</span>
<textarea class="form-control" name="message" placeholder="Project Description"></textarea>
</div>
</div>
</div>

<div class="alert alert-success" role="alert" id="success_message" style="display:none;">Success
<i class="glyphicon glyphicon-thumbs-up"></i>
Thanks for contacting us, we will get back to you shortly.</div>

<div class="alert alert-error" role="alert" id="error_message" style="display:none;">Error
<i class="glyphicon glyphicon-thumbs-up"></i>
There was a problem sending your message. Please try again in a few minutes.</div>

<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-4">
<button type="submit" class="btn btn-warning">Send
<span class="glyphicon glyphicon-send"></span>
</button>
</div>
</div>

</fieldset>
</form>
</div>
</div>

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/sweetalert2/6.1.0/sweetalert2.min.js"></script>

<script>
$('#contact-form').submit(function(event) {
var form = $(this);
$.ajax({
type: 'POST',
url: form.attr('action'),
data: form.serialize(),
dataType: 'json',
encode: true
})
.done(function(data) {
swal('Thank You!', data.message, 'success')
}).fail(function(data) {
console.log(data);
if (typeof data.responseJSON !== 'undefined' && typeof data.responseJSON.details !== 'undefined') {
swal('Oops...', data.responseJSON.details, 'error');
} else {
swal('Oops...', 'We\'re sorry, something went wrong. Please try again in a few minutes.', 'error');
}
});
event.preventDefault();
});
</script>
</body>

0 comments on commit 354b05c

Please sign in to comment.