Repopulate cloned Item from MySQL to the form

Repopulate cloned Item from MySQL to the form


Hi,

I am using the below code to save the industry to company_industry table (

I wanted to know now how can I populate again the data when editing the company so it will appear in <options> element? I thought of using PHP with just normal loop but I'll still have a problem with the btnAddIndustry and the delete button because only the first cboIndustry will have the add and all others will have the delete button.

this is what I though to have:

  1. <?php
  2.     $mysql_command = "CALL sp_populate_company_industry(:param_company_guid)";
  3.     $mysql_query = $mysql_connection->prepare($mysql_command);
  4.     $mysql_query->bindParam(':param_company_guid', $company_guid, PDO::PARAM_STR);
  5.     $mysql_query->execute();

  6.     while($mysql_row = $mysql_query->fetch())
  7.     {
  8.          here is the populate code;
  9.     }
  10. ?>

How can I have this done?

here is my current code for the new company entry:
  1. <tr>
  2.     <td valign="middle"><?php if (!$_GET["id"]) { ?>Industry<?php } ?></td>
  3.     <td valign="middle"><?php if (!$_GET["id"]) { ?>:<?php } ?></td>
  4.     <td valign="middle">         
  5.         <div id="container">
  6.         <select id="cboIndustry" name="cboIndustry[]" style="width: 80%" <?php if (!$_GET["id"]) { ?>required<?php } ?>>
  7.             <option value="" selected>[Industry..]</option>
  8.             <?php
  9.                 $mysql_command = "CALL sp_populate_industry()";
  10.                 $mysql_query = $mysql_connection->prepare($mysql_command);

  11.                 $mysql_query->execute();

  12.                 while($mysql_row = $mysql_query->fetch())
  13.                 {
  14.             ?>
  15.             <option value="<?php echo $mysql_row['industry_id']; ?>"><?php echo $mysql_row['industry_name']; ?></option>
  16.             <?php } ?>
  17.         </select>
  18.         <button id="btnAddIndustry" type="button" style="width:150px">Add More</button>
  19.         </div>
  20.     </td>
  21. </tr>

  1. <script type="text/javascript">
  2.     $(function() {
  3.         var counter = 0;
  4.         $('#btnAddIndustry').click(function() {
  5.             counter++;
  6.             var newField = $('#cboIndustry').clone().
  7.                 attr({id: 'cboIndustry_' + counter,
  8.                     name: 'cboIndustry[]'});
  9.             $('#container').append(newField).
  10.             append('<button id="btnDeleteIndustry" type="button" class="delete">Delete</button>');
  11.         });
  12.         $('#container').on('click', 'button.delete', function()
  13.         { $(this).prev().addBack().remove(); });
  14.     });
  15. </script>