Send array of <input> name/value pairs via AJAX

by alano

HTML

<!--I have renamed these so they are unique-->
<input type="hidden" name="produtoq1" value="25" size="3" maxlength="3" />
<input type="hidden" name="produtoq2" value="20" size="3" maxlength="3" />
<input type="hidden" name="produtoq3" value="14" size="3" maxlength="3" />

JavaScript

//This code will create an array of name/value pairs
//for sending to server via AJAX

prodsMarcados = [];  //shorthand array constructor!
$("[name^='produtoq']").each(function(){
    prodsMarcados.push({name: this.name, value: this.value});
});
alert($.param(prodsMarcados));

//prodsMarcados is now an array of key/value pairs (i.e. JSON)
//you should be able to send it to your server via AJAX same as before, but  tell server to treat it as JSON data