Skip to content

Instantly share code, notes, and snippets.

Created January 28, 2016 17:03
Show Gist options
  • Save anonymous/0ebcceb2e9771668e233 to your computer and use it in GitHub Desktop.
Save anonymous/0ebcceb2e9771668e233 to your computer and use it in GitHub Desktop.
JS Bin bv submit // source https://jsbin.com/rupafo
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="bv submit">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://rawgit.com/rickharrison/validate.js/master/validate.min.js"></script>
<script src="https://rawgit.com/larryosborn/JSONP/master/lib/jsonp.min.js"></script>
<!--<script src="https://rawgit.com/OscarGodson/JSONP/master/JSONP-min.js"></script>-->
<script>
function serialize(form){if(!form||form.nodeName!=="FORM"){return }var i,j,q=[];for(i=form.elements.length-1;i>=0;i=i-1){if(form.elements[i].name===""){continue}switch(form.elements[i].nodeName){case"INPUT":switch(form.elements[i].type){case"text":case"hidden":case"password":case"button":case"reset":case"submit":q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value));break;case"checkbox":case"radio":if(form.elements[i].checked){q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value))}break;case"file":break}break;case"TEXTAREA":q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value));break;case"SELECT":switch(form.elements[i].type){case"select-one":q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value));break;case"select-multiple":for(j=form.elements[i].options.length-1;j>=0;j=j-1){if(form.elements[i].options[j].selected){q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].options[j].value))}}break}break;case"BUTTON":switch(form.elements[i].type){case"reset":case"submit":case"button":q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value));break}break}}return q.join("&")};
</script>
<style id="jsbin-css">
/*
notes:
intercept form before submission via client side validation
if(fail) {
get user to correct errors and try again
}
if(success){
submit form ready for server side validation with method="get" and token &Action=preview
if(HasErrors="true"){
display errors returned from the .json under FormErrors.FieldErrors
and get the user to correct mistakes: this will lead the user back through the client side validation process, but unless they change something that was previously accepted, then they will automatically succeed with this submission step, and back to the server side validation check.
}
if(HasErrors="false"){
change method="get" to method="post", and &Action=preview to &Action=submit, then submit the form - form will be submitted to bazaarvoice. :)
}
}
*/
</style>
</head>
<body>
<form id="feedback_form" name="feedback_form" action="https://stg.api.bazaarvoice.com/data/submitreview.json?" method="GET" target="_blank">
<small>all this will be hidden and prepopulated</small><br>
<small>__________________________________</small><br><br>
passkey: <input type="text" name="PassKey" value="29jjackvwjqqzmrrek8455ys"><br>
apiversion: <input type="text" name="apiversion" value="5.4"><br>
locale: <input type="text" name="locale" value="fi_FI"><br>
ProductId: <input type="text" name="ProductId" value="12345"><br>
<!--Callback: <input type="text" name="Callback" value="jsfunct"><br>-->
action: <input id="action" type="text" name="action" value="Preview"><br>
<small>__________________________________</small><br><br><br>
UserNickname: <input type="text" name="usernickname"><br>
ReviewText: <textarea type="text" name="ReviewText"></textarea><br>
Title: <input type="text" name="Title"><br>
Rating 1: <input type="radio" name="Rating" value=1><br>
Rating 2: <input type="radio" name="Rating" value=2><br>
Rating 3: <input type="radio" name="Rating" value=3><br>
Rating 4: <input type="radio" name="Rating" value=4><br>
Rating 5: <input type="radio" name="Rating" value=5><br>
agree: <input type="checkbox" name="agreedtotermsandconditions">
<input id="sub" type="submit" value="Submit">
</form>
<iframe id="frame_output" name="frame_output" src=""></iframe>
<div id="check2"></div>
<div id="check3"></div>
<script id="jsbin-javascript">
var validator = new FormValidator('feedback_form', [{
name: 'usernickname',
display: 'required',
rules: 'min_length[4]'
},{
name: 'Title',
display: 'required',
rules: 'required'
},{
name: 'ReviewText',
display: 'required',
rules: 'min_length[5]'
}], function(errors, event) {
if (errors.length < 1) {
console.log("passed client side validation");
// this is intentional
event.preventDefault();
var baseURI = "https://stg.api.bazaarvoice.com/data/submitreview.json";
var queryString = serialize(document.forms[0]);
var fullURI = baseURI+'?'+queryString;
JSONP({
url: fullURI,
success: function(data) {
if(data.HasErrors){
console.log("noooo");
}
if(!data.HasErrors){
console.log("surprise Mothafucker!!");
var act = document.getElementById("action");
act.value = "submit";
var queryString2 = serialize(document.forms[0]);
var fullURI2 = baseURI+'?'+queryString2;
var form = document.createElement("form");
form.id = "super";
form.setAttribute("method", "post");
form.setAttribute("action", fullURI2);
document.body.appendChild(form);
form.submit();
/*
var act = document.getElementById("action");
act.value = "submit";
var meth = document.getElementById("feedback_form");
meth.method = "POST";
meth.style.background="red";
var queryString2 = serialize(document.forms[0]);
var fullURI2 = baseURI+'?'+queryString2;
JSONP({
url: fullURI2,
callbackName:'nullcb'
});
*/
}
}
});
/*
JSONP(fullURI,function(json){
//console.log(json.HasErrors);
if(json.HasErrors){
console.log("noooooo");
JSONP(fullURI,function(json){
console.log("noooo again");
});
}
if(!json.HasErrors){
console.log("surprise Mothafucker!!");
var act = document.getElementById("action");
act.value = "submit";
var meth = document.getElementById("feedback_form");
meth.method = "post";
meth.style.background="red";
var queryString2 = serialize(document.forms[0]);
var fullURI2 = baseURI+'?'+queryString2;
JSONP(fullURI,'nullcb',function(json){
console.log("maybe success");
});
}
});
*/
}
});
/*
var but = document.getElementById("sub");
window.addEventListener("mousedown", function(){
console.log("ddd");
document.getElementById("check3").innerHTML = queryString;
});
*/
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta name="description" content="bv submit">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://rawgit.com/rickharrison/validate.js/master/validate.min.js"><\/script>
<script src="https://rawgit.com/larryosborn/JSONP/master/lib/jsonp.min.js"><\/script>
<\!--<script src="https://rawgit.com/OscarGodson/JSONP/master/JSONP-min.js"><\/script>-->
<script>
function serialize(form){if(!form||form.nodeName!=="FORM"){return }var i,j,q=[];for(i=form.elements.length-1;i>=0;i=i-1){if(form.elements[i].name===""){continue}switch(form.elements[i].nodeName){case"INPUT":switch(form.elements[i].type){case"text":case"hidden":case"password":case"button":case"reset":case"submit":q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value));break;case"checkbox":case"radio":if(form.elements[i].checked){q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value))}break;case"file":break}break;case"TEXTAREA":q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value));break;case"SELECT":switch(form.elements[i].type){case"select-one":q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value));break;case"select-multiple":for(j=form.elements[i].options.length-1;j>=0;j=j-1){if(form.elements[i].options[j].selected){q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].options[j].value))}}break}break;case"BUTTON":switch(form.elements[i].type){case"reset":case"submit":case"button":q.push(form.elements[i].name+"="+encodeURIComponent(form.elements[i].value));break}break}}return q.join("&")};
<\/script>
</head>
<body>
<form id="feedback_form" name="feedback_form" action="https://stg.api.bazaarvoice.com/data/submitreview.json?" method="GET" target="_blank">
<small>all this will be hidden and prepopulated</small><br>
<small>__________________________________</small><br><br>
passkey: <input type="text" name="PassKey" value="29jjackvwjqqzmrrek8455ys"><br>
apiversion: <input type="text" name="apiversion" value="5.4"><br>
locale: <input type="text" name="locale" value="fi_FI"><br>
ProductId: <input type="text" name="ProductId" value="12345"><br>
<\!--Callback: <input type="text" name="Callback" value="jsfunct"><br>-->
action: <input id="action" type="text" name="action" value="Preview"><br>
<small>__________________________________</small><br><br><br>
UserNickname: <input type="text" name="usernickname"><br>
ReviewText: <textarea type="text" name="ReviewText"></textarea><br>
Title: <input type="text" name="Title"><br>
Rating 1: <input type="radio" name="Rating" value=1><br>
Rating 2: <input type="radio" name="Rating" value=2><br>
Rating 3: <input type="radio" name="Rating" value=3><br>
Rating 4: <input type="radio" name="Rating" value=4><br>
Rating 5: <input type="radio" name="Rating" value=5><br>
agree: <input type="checkbox" name="agreedtotermsandconditions">
<input id="sub" type="submit" value="Submit">
</form>
<iframe id="frame_output" name="frame_output" src=""></iframe>
<div id="check2"></div>
<div id="check3"></div>
</body>
</html>
</script>
<script id="jsbin-source-css" type="text/css">/*
notes:
intercept form before submission via client side validation
if(fail) {
get user to correct errors and try again
}
if(success){
submit form ready for server side validation with method="get" and token &Action=preview
if(HasErrors="true"){
display errors returned from the .json under FormErrors.FieldErrors
and get the user to correct mistakes: this will lead the user back through the client side validation process, but unless they change something that was previously accepted, then they will automatically succeed with this submission step, and back to the server side validation check.
}
if(HasErrors="false"){
change method="get" to method="post", and &Action=preview to &Action=submit, then submit the form - form will be submitted to bazaarvoice. :)
}
}
*/</script>
<script id="jsbin-source-javascript" type="text/javascript">var validator = new FormValidator('feedback_form', [{
name: 'usernickname',
display: 'required',
rules: 'min_length[4]'
},{
name: 'Title',
display: 'required',
rules: 'required'
},{
name: 'ReviewText',
display: 'required',
rules: 'min_length[5]'
}], function(errors, event) {
if (errors.length < 1) {
console.log("passed client side validation");
// this is intentional
event.preventDefault();
var baseURI = "https://stg.api.bazaarvoice.com/data/submitreview.json";
var queryString = serialize(document.forms[0]);
var fullURI = baseURI+'?'+queryString;
JSONP({
url: fullURI,
success: function(data) {
if(data.HasErrors){
console.log("noooo");
}
if(!data.HasErrors){
console.log("surprise Mothafucker!!");
var act = document.getElementById("action");
act.value = "submit";
var queryString2 = serialize(document.forms[0]);
var fullURI2 = baseURI+'?'+queryString2;
var form = document.createElement("form");
form.id = "super";
form.setAttribute("method", "post");
form.setAttribute("action", fullURI2);
document.body.appendChild(form);
form.submit();
/*
var act = document.getElementById("action");
act.value = "submit";
var meth = document.getElementById("feedback_form");
meth.method = "POST";
meth.style.background="red";
var queryString2 = serialize(document.forms[0]);
var fullURI2 = baseURI+'?'+queryString2;
JSONP({
url: fullURI2,
callbackName:'nullcb'
});
*/
}
}
});
/*
JSONP(fullURI,function(json){
//console.log(json.HasErrors);
if(json.HasErrors){
console.log("noooooo");
JSONP(fullURI,function(json){
console.log("noooo again");
});
}
if(!json.HasErrors){
console.log("surprise Mothafucker!!");
var act = document.getElementById("action");
act.value = "submit";
var meth = document.getElementById("feedback_form");
meth.method = "post";
meth.style.background="red";
var queryString2 = serialize(document.forms[0]);
var fullURI2 = baseURI+'?'+queryString2;
JSONP(fullURI,'nullcb',function(json){
console.log("maybe success");
});
}
});
*/
}
});
/*
var but = document.getElementById("sub");
window.addEventListener("mousedown", function(){
console.log("ddd");
document.getElementById("check3").innerHTML = queryString;
});
*/
</script></body>
</html>
/*
notes:
intercept form before submission via client side validation
if(fail) {
get user to correct errors and try again
}
if(success){
submit form ready for server side validation with method="get" and token &Action=preview
if(HasErrors="true"){
display errors returned from the .json under FormErrors.FieldErrors
and get the user to correct mistakes: this will lead the user back through the client side validation process, but unless they change something that was previously accepted, then they will automatically succeed with this submission step, and back to the server side validation check.
}
if(HasErrors="false"){
change method="get" to method="post", and &Action=preview to &Action=submit, then submit the form - form will be submitted to bazaarvoice. :)
}
}
*/
var validator = new FormValidator('feedback_form', [{
name: 'usernickname',
display: 'required',
rules: 'min_length[4]'
},{
name: 'Title',
display: 'required',
rules: 'required'
},{
name: 'ReviewText',
display: 'required',
rules: 'min_length[5]'
}], function(errors, event) {
if (errors.length < 1) {
console.log("passed client side validation");
// this is intentional
event.preventDefault();
var baseURI = "https://stg.api.bazaarvoice.com/data/submitreview.json";
var queryString = serialize(document.forms[0]);
var fullURI = baseURI+'?'+queryString;
JSONP({
url: fullURI,
success: function(data) {
if(data.HasErrors){
console.log("noooo");
}
if(!data.HasErrors){
console.log("surprise Mothafucker!!");
var act = document.getElementById("action");
act.value = "submit";
var queryString2 = serialize(document.forms[0]);
var fullURI2 = baseURI+'?'+queryString2;
var form = document.createElement("form");
form.id = "super";
form.setAttribute("method", "post");
form.setAttribute("action", fullURI2);
document.body.appendChild(form);
form.submit();
/*
var act = document.getElementById("action");
act.value = "submit";
var meth = document.getElementById("feedback_form");
meth.method = "POST";
meth.style.background="red";
var queryString2 = serialize(document.forms[0]);
var fullURI2 = baseURI+'?'+queryString2;
JSONP({
url: fullURI2,
callbackName:'nullcb'
});
*/
}
}
});
/*
JSONP(fullURI,function(json){
//console.log(json.HasErrors);
if(json.HasErrors){
console.log("noooooo");
JSONP(fullURI,function(json){
console.log("noooo again");
});
}
if(!json.HasErrors){
console.log("surprise Mothafucker!!");
var act = document.getElementById("action");
act.value = "submit";
var meth = document.getElementById("feedback_form");
meth.method = "post";
meth.style.background="red";
var queryString2 = serialize(document.forms[0]);
var fullURI2 = baseURI+'?'+queryString2;
JSONP(fullURI,'nullcb',function(json){
console.log("maybe success");
});
}
});
*/
}
});
/*
var but = document.getElementById("sub");
window.addEventListener("mousedown", function(){
console.log("ddd");
document.getElementById("check3").innerHTML = queryString;
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment