Skip to content

Instantly share code, notes, and snippets.

@chukShirley
Last active August 29, 2015 14:08
Show Gist options
  • Save chukShirley/b83705d57830411a0e2b to your computer and use it in GitHub Desktop.
Save chukShirley/b83705d57830411a0e2b to your computer and use it in GitHub Desktop.
Append params to form before submit
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<form name="real" id="real" method="POST" action="submit.php">
<input type="submit" value="submit real form">
</form>
<div class="toBeAdded">
<form><input type="text" name="durf1" id="durf1" value="test1"></form>
<form><input type="text" name="durf2" id="durf2" value="test2"></form>
<form><input type="text" name="durf3" id="durf3" value="test3"></form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('#real').on('submit',function(event){
// Assemble an array of input elements
var dataToBeAdded = $('.toBeAdded form input');
// Optionally include blank elements
var includeBlankElements = false;
$.each(dataToBeAdded,function(key,value){
var name = $(this).prop('name');
var value = $(this).val();
if (includeBlankElements === true || (includeBlankElements === false && value !== "")) {
$('#real').append('<input name="'+name+'" value="'+value+'">');
}
});
return true;
});
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment