开发者

Comparing form fields with data base fields and highlighting the form fields

Can you help me in the following two scenarios (PHP+MYSQL)

Scenario1: I need to compare the HTML Form field values with the Database field values and highlight the form fields in some color whose values are different from database values before 开发者_开发知识库 submitting the form (to alert the user).

Scenario2:

On loading the form, i need to compare the values present in 2 different database tables (tables are having different column names but the information is same). the fields which are not same needs to be highlighted in the html form to indicate the users that the master data is varying from secondary data.

Could you help me which is the efficient way of doing this (comparison and highlighting the form values).

thanks in advance Naveen


Scenario 1

<form method="post">
  <? foreach ($fields as $field) : ?>
    <? if (in_array($diff_fields, $field)) : ?>
    <div style="background-color:red">
    <? else : ?>
    <div>
    <? endif; ?>
      <input type="text" value="<?= $record[$field] ?>"/>
    </div>
  <? endforeach; ?>
</form>

$fields = array('id', 'name', 'created_at');
$diff_fields = array();

$record = fetch_from_db($record_id);

foreach ($fields as $field) {
  if (isset($record[$field]) && $record[$field] != $_POST[$field]) {
    $diff_fields[] = $field;
  }
}


Since you want to handle scenario one before you submit the form, you'll need to use JavaScript to do the comparison. If you know the structure of the form before you generate the page, it should be east to write a series of comparisons using jQuery, change(), and hidden fields in the form.

For scenario two it depends a little on your definition of "efficient". If you want it to use the least server resource, you could send the page back with the two database tables entered into well named tables, and again use JavaScript to highlight the differences. If you want a solution that isn't dependent on JS running on the client's browser you could do the comparison in a query to MySQL. Running the comparison in PHP itself is probably the easiest to code and therefore most efficient for programmer time, but slowest execution (if that matters depends on several things like table size).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜