Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

checking whether a form value has changed upon submit

I'm creating a web app with Codeigniter, and I've created some edit forms (which pull current values from a mysql database). The user can edit the current database data by editing the data in the form.

What I want to do is perform certain actions if the user changes certain values. So, I don't just want to perform the action when a field has a certain value, but only at the point when the user changes the value and submits the form. (Specifically, when the user indicates that she's performed a certain task by changing a value from "no" to "yes", then I want to do things like set a timestamp for the completion of the task, etc.)

I've tried googling a solution, but I'm having trouble finding what I need. Can anyone point me in the right direction?

like image 361
Christina Huggins Ramey Avatar asked May 29 '12 01:05

Christina Huggins Ramey


People also ask

How can I show form data after submitting?

The formtarget attribute specifies a name or a keyword that indicates where to display the response that is received after submitting the form. The formtarget attribute overrides the target attribute of the <form> element. Note: The formtarget attribute is new for the <input> element with type="submit" in HTML5.

How do you take values from forms?

This can be done by passing the event in the onsubmit field. We can then use FormData to retrieve the values of this exact form by referencing the SubmitEvent object. const check = (e) => { const form = new FormData(e. target); const formula = form.


2 Answers

I haven't used CodeIgniter, but I've certainly done what you're doing in pure PHP-based sites.

I've followed two ways of thinking, in different projects.

Strategy #1: Multiple writes are cheap.

If a user clicks "Submit" rather than "Cancel", they've changed at least one field. So the cost of doing an UPDATE table SET name=%s,email=%s,gender=%s WHERE id=%d isn't much more than a simple UPDATE table SET gender=%s WHERE id=%d. If you're going to the expense of a WHERE and a write, making the write a few extra fields doesn't matter, especially with the frequency that it'll happen.

So: don't worry about it, just update everything with what you get back in the form. If you overwrite a field with the same data, it doesn't matter. When it comes down to it, you want your database to reflect everything that came back in the form, regardless of what was in the db before.

Strategy #2: Use a session variable.

If you've populated the form with current data, you've already likely pulled it into an array. So copy the array into $_SESSION, and compare the fields after the POST.

Strategy 1 is easier to program, but does use slightly more database CPU and bandwidth. Strategy 2 is has slightly less database impact at the expense of quite a bit more CPU used on your web server, and it's more prone to development error.

I still don't know which way is better, but the arguments for both seem valid. These days, I tend to go for whatever solution is the most simple. But I realize that it's easier to scale your web cluster than your database cluster, so if you're developing something that will be very large, it's probably better to put more effort into optimizing things in favour of your database rather than your web servers.

Note that if you're storing your session data in a database instead of temp files, then #2 is actually more costly in terms of database server impact.

like image 198
ghoti Avatar answered Oct 30 '22 23:10

ghoti


You're saying that the users can edit entries from a database, so just send the record id as a hidden input field.

By the time the user submits the form, you retrieve the database record using the hidden field and make the necessary comparisons.

Btw, to prevent users from trying to modify other's records it's advisable to add a checksum to the id field that only you can verify, something that can be done using hash_hmac. Alternatively, you could verify the record ownership if they're logged in.

like image 32
Ja͢ck Avatar answered Oct 30 '22 22:10

Ja͢ck