phildeman has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I hope I can get help with this problem I have.

I am trying to compare the value in a textarea to the value in a hidden field. I am doing this to avoid updating the DB table, if there are no changes. The data is stored in a MySQL table. I am using DBIx::Class to interact with the MySQL table.

Initially, I retrieve the data from the database and populate the textarea in a form. In addition, I assign the same value in a hidden field to do a comparison, later. If the textarea contains bullets, it will not return the appropriate return value.

I am using Catalyst with Mason. In addition I am using CKEditor with the textarea fields.

HTML CODE

. . . <td>Attending:</td> <td><textarea class="ckeditor" name="attending_msg" id="attendin +g_msg"><% $obj->attending_msg %></textarea></td> </tr> <tr> <td>Not Attending:</td> <td><textarea class="ckeditor" name="notattending_msg" id="nota +ttending_msg"><% $obj->notattending_msg %></textarea></td> </tr> <tr> <td colspan="2"> <p>&nbsp;</p> <input type='hidden' name='o_attending_msg' value="<% $obj +->attending_msg %>"> <input type='hidden' name='o_notattending_msg' value="<% $ +obj->notattending_msg %>"> </td> </tr> . . .

PERL CODE

method set_email_content($params, $username) { my $dt = $self->get_datetime(); my $posted_on = $dt->ymd . " " . $dt->hms; if(($params->{ 'attending_msg' } eq $params->{ 'o_attending_msg +'}) && ($params->{ 'notattending_msg' } eq $params->{ 'o_notattendi +ng_msg' })) { return 0; } else { my $obj = $schema->resultset( 'MyTable' )->search({ MID => + 1})->single; $obj->attending_msg( $params->{ 'attending_msg' } ); $obj->notattending_msg( $params->{ 'notattending_msg' } ); $obj->updated_by( $username ); $obj->updated_on( $posted_on ); $obj->update; return 1; } }

If I submit the form, with unchanged values it continues to update the database, because it assumes the textarea value is not equal to the hidden field value. This only happens when there is an unordered list,

However, it works fine with other html tags and/or simple text. Has anyone experience this before? If yes, please let me know how you resolved it.

Thanks for your help.

-Phil-

Replies are listed 'Best First'.
Re: Compare Textarea Value to Hidden Field Value
by Corion (Patriarch) on Apr 01, 2016 at 16:49 UTC

    Have you printed the values your script receives to make really certain the values are not different?

    Maybe there are whitespace differences, like a different type of newlines?

    Maybe the encoding has changed, if your lists contain non-ascii characters?

    Maybe it would be easier to maintain a version number and increment that if changes to the participation messages are made?

Re: Compare Textarea Value to Hidden Field Value
by Your Mother (Archbishop) on Apr 01, 2016 at 19:23 UTC

    I would guess CKEditor is changing/reformatting the HTML for you. If you really want to allow HTML input, you should normalize it as that is the only way to decide <p>OHAI!</p> is equal to <p>\nOHAI!\n</p>, which it is, as far as HTML is concerned, and unless you are doing wonky stuff with CSS/JS to create significance in otherwise meaningless white space. This is not quite as easy as it might sound, but it's doable if that's really what you're after.

    Personally, I would choose client side / JS tracking of change instead, and not submit unless there is a change, then let the backend always update. It's "hackabe" in the sense a user could turn off JS or POST directly with any number of tools but if you have the permission and data checks you should have anyway on the backend the worst the "hack" could do is waste a couple tenths of a second of clock time. The approach would be cheaper than creating, normalizing, and validating 4 or more HTML document fragments on every submit.

    Update: another approach. Apply CKEditor to the hidden field so it gets the same processing as the visible one by instead of using hidden use something like <textarea style="display:none" ...>.

    Update: s/of/off/; s/seconds/second/; :P

Re: Compare Textarea Value to Hidden Field Value
by tangent (Parson) on Apr 01, 2016 at 22:25 UTC
    As Your Mother points out, this is much easier to implement on the client side. If you use jQuery your hidden field can be reduced to a boolean value and all you need do is something like:
    $("#id_of_textarea").change( function() { $("#id_of_textarea_changed").val(1); });
    I'm sure this would be easy enough in plain Javascript also.