Make your contact form submit into UserVoice

Let's say you have a contact form of your marketing website, and you'd like to make it submit into UserVoice as a Ticket.

The best solution is to just use our Contact widget -- it has built-in instant answers and in general is quite awesome.  But if you can't use that, for instance if your contact form has really awesome styles that you don't want to get rid of, there is hope!

Simply add some JavaScript to your web page so that you submit the ticket to our API via JSONP.  Check it out:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
<!DOCTYPE html>
<head>
<title>Demo Contact Form - Submit a ticket to UV via JSONP</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://jquery-jsonp.googlecode.com/files/jquery.jsonp-2.2.0.min.js"></script>
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" />
<style type="text/css"> #contact_form { margin: 20px auto; width: 500px; } </style>
</head>
 
<body>
<div id="contact_form">
<form id="test_form" name="contact" method="post" action="http://whatever.you.want.com/up_to_you" class="well">
<label for="name">Name</label>
<input type="text" name="name" id="name" value="" class="span3" />
<label for="email">Email</label>
<input type="email" name="email" id="email" value="" class="span3" />
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" value="" class="span3" />
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<div class="form-actions">
<button class="btn btn-primary" type="submit">Send Ticket</button>
<button class="btn">Cancel</button>
</div>
</form>
</div>
<script type="text/javascript">
$('#test_form').submit(function(evt) {
evt.preventDefault(); // Don't do default action
 
// Replace this with your subdomain!
var uvSubdomain = "initech";
// Create an API client (non-trusted) within the UserVoice settings/channels section. Paste the key only here.
var uvKey = "5JVoVrAGdl4pMkcEkIeIDA";
// Grab the data we need to send
var message = $('#message').val();
var subject = $('#subject').val();
var name = $('#name').val();
var email = $('#email').val();
 
// Execute the JSONP request to submit the ticket
$.jsonp({
url: 'https://' + uvSubdomain + '.uservoice.com/api/v1/tickets/create_via_jsonp.json?callback=?',
data: {
client: uvKey,
ticket: {
message: message,
subject: subject
},
name: name,
email: email
},
success: function(data) {
alert('Successfully submitted ticket!'); // You might want to redirect the user here, or show a stylized message to them.
},
error: function(d, msg) {
alert("Error"); // Darn -- something went wrong. You might want to display a message to the user.
}
});
return false;
});
</script>
</body>
</html>
view raw contact.html This Gist brought to you by GitHub.

Feedback and Knowledge Base