tags:

views:

85

answers:

2

Hi,

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

A: 

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;
  }
}
knoopx
A: 

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).

acrosman
is there any other alternative with out using Jquery
naveen kotla
If you want to process client-side before submitting form, JavaScript is the tool to use. There are certainly other libraries you could use, or you could go without a library if you wanted. I just suggest jQuery because it makes JS easier to work with.
acrosman