Created
July 1, 2013 21:09
-
-
Save rosschapman/5904657 to your computer and use it in GitHub Desktop.
Post form data to Marketo w/o page reload (JavaScript)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function postToMarketo() { | |
var form = $('<form class="marketo-form" method="post" enctype="application/x-www-form-urlencoded" action="http://info.domain.com/index.php/leadCapture/save" id="mktForm_ID" name="mktForm_ID">'); | |
var inputArr = {}; | |
var selectedArr = {}; | |
var key; | |
var inputs = $('form.leadgen input'); | |
var selected = $('form.leadgen option[selected]'); | |
var iframe = $('<iframe style="display:none;" id="new-form" src="/marketo_success.html"></iframe>'); | |
// rebuild inputs and append to new form | |
inputs.each(function() { | |
inputArr[this.id] = $(this).val(); | |
}); | |
for(var key in inputArr) { | |
var hiddenField = $("<input>"); | |
hiddenField.attr({ | |
type: "hidden", | |
name: key, | |
value: inputArr[key] | |
}); | |
form.append(hiddenField); | |
} | |
// rebuild dropdown selections and append to new form | |
selected.each(function() { | |
var parentName = $(this).parent().attr('name'); | |
var optionField = $('<select><option>'); | |
optionField.attr('name',parentName); | |
optionField.children().attr({ | |
value: $(this).val(), | |
selected: "selected" | |
}); | |
form.append(optionField); | |
}); | |
// put form into iframe | |
iframe.load(function() { | |
iframe.contents().find('body').append(form); | |
}); | |
// put iframe as results | |
$('#registration').append(iframe); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment