badsmell badsmell - 3 months ago 12
Ajax Question

How to have input field outside form tags submit using serialzied AJAX POST

I have a a form that submits five times using some AJAX. I have a value outside these form tags, referer_fname, I would like to append to each AJAX Post. Any help would be appreciated!

Below is my script so far:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"> </script>
<!--serial submit ajax-->
<script>
function mySubmit(){
var myForms = document.getElementsByTagName("form");
for (var t = 0; t<myForms.length; t++){
$.post("http://", $(myForms).eq(t).serialize(), function (data, status) {
if (status === "success"){
window.location.href= "http://redirect.com";
}
}
)
}
}

</script>


Thank you again!

Answer

You can append to the serialized form fields output (see this the accepted answer on this post for reference)

function mySubmit(){
  var myForms = $("form");
  myForms.each(function(index) {
    var form = myForms.eq(index);
    var serializedForm = form.serialize();
    serializedForm += '&referer_fname='+$('#refererFrame').val();
    $.post("https://", serializedForm, function (data, status) {
      if (status === "success"){
       window.location.href= "http://redirect.com";
      }  
    });   
  });
}