I have a series of table rows like this:
<cfloop query="qryPlayer">
(...snip...)
<input data-playerid="#PlayerID#" name="PlayerSort" value="#PlayerSort#">
(...snip...)
</cfloop>
At the bottom of the form, I have a single field:
<input type="hidden" name="PlayerIDs"> <!--- Populated by js --->
Whenever the user changes a value in any row, I populate PlayerIDs with a list of the primary keys associated with the fields that needs to be updated:
$('input[name=PlayerSort]').change(function() {
$('input[name=Save]').show();
var arrPlayerID = [];
$('input[name=PlayerSort]').each(function() {
if ($(this).val()) {
arrPlayerID.push($(this).data('playerid'));
}
});
$('input[name=PlayerIDs]').val(arrPlayerID.join(','));
});
Then when the user presses the Save button, I update all the fields that have values in them.
Q: Is that the right thing for me to be doing? It works, but have I forgotten some fundamental way of handling a form submit?
BTW, the client doesn’t want it to be AJAXified because he wants to give the user the ability to press reset and start over again. So, enter a bunch of values, click Save, and have ColdFusion update all the PlayerSort values associated with form.PlayerIDs.
<cfloop list="#form.PlayerIDs#" index="local.PlayerID">
<cfset local.Index = local.Index + 1>
UPDATE dbo.Player SET
PlayerSort = <cfqueryparam cfsqltype="cf_sql_integer" value="#ListGetAt(form.PlayerSort,local.Index)#">
WHERE PlayerID = <cfqueryparam cfsqltype="cf_sql_integer" value="#local.PlayerID#">;
</cfloop>
I think your approach is fine. It could, however, be done without JavaScript which would reduce and simplify your code a bit as well as ensure it works on the odd chance that the user has JavaScript disabled.
Use this for your form code:
Then your action code would be this: