0

So I have a form that performs a query and populates the form with the results of that query in the form of file paths, when it was last updated etc.... It also displays 5 different audience checkboxes for each entry that is populated, and these checkboxes are checked if they have values in the database (there are 5 possible audience values - hence 5 checkboxes - and one entry can have multiple audience values so multiple checkboxes can be checked in one entry). So far I have all of that working, the page displays all the correct entries and their audience checkboxes from my mysql database. Now what I need to do is find a way to detect if there is a change in those checkboxes from what was originally retrieved from the database, and if there is a change then the values are inserted into the database. How do I go about doing this? Javascript? Compare arrays in PHP? Here is part of my while loop that populates the form with check boxes:

printf("\t<tr class='recordrow'>
        \n\t\t<td><a class='page_modal' href='#mWindow%s' target='_blank'>%s</a></td>
        \n\t\t<td class='ppath'>%s</a></td>
        \n\t\t<td>%s</td>
        \n\t\t<td><input type='checkbox' name='audience[]' $currentcheckbox/><div class='page_dialog' id='mWindow%s'><img src='exit.png' class='close' alt='close'/><img src='newwin.jpg' alt='new window'/><div id='content_section'>%s</div></div> </td>
        \n\t\t<td><input type='checkbox' name='audience[]' $prospectivecheckbox/></td>
        \n\t\t<td><input type='checkbox' name='audience[]' $facultycheckbox/></td>
        \n\t\t<td><input type='checkbox' name='audience[]' $staffcheckbox/></td>
        \n\t\t<td><input type='checkbox' name='audience[]' $externalcheckbox/></td>
        \t</tr>",$folder_content["cms_id"],$folder_content["display_name"],$page_path,$update,$folder_content["cms_id"],$folder_content["path"],$folder_content["cms_id"],$folder_content["content"]);

1 Answers1

0

It depends on when you want to detect if there's a change: when the form is submitted, or when a checkbox is clicked in real time. If the latter, just do an ajax request on a javascript event:

jQuery checkbox change and click event

http://html.net/tutorials/javascript/lesson21.php

If the former, I wouldn't bother checking whether the checkboxes have been changed. Just override the values in the database with whatever was submitted in the form. In most cases, I've found, it's more confusing and liable for errors to try and detect a change before updating a record, and I haven't seen any performance benefits to it.

If you simply have to only perform updates when there's a change, get an array of you're original checkbox values and compare them to the new ones using array_diff.

$values = get_current_values_somehow();
$remove = array_diff($values, $_GET['audience']); // Array of entries to remove
$add = array_diff($_GET['audience'], $values); // Array of entries to add

if (count($remove) > 0) {
    // Remove from database
}

if (count($add) > 0) {
    // Add to database
}
Community
  • 1
  • 1
ethan
  • 985
  • 6
  • 10
  • I should have mentioned that I want to detect if there have been changes when the form is submitted... Unfortunately I can't just override the values in the database unless there is a change on the form because we need the timestamps –  Jan 15 '14 at 20:43