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:
- <?php
- $mysql_command = "CALL sp_populate_company_industry(:param_company_guid)";
- $mysql_query = $mysql_connection->prepare($mysql_command);
- $mysql_query->bindParam(':param_company_guid', $company_guid, PDO::PARAM_STR);
- $mysql_query->execute();
- while($mysql_row = $mysql_query->fetch())
- {
- here is the populate code;
- }
- ?>
How can I have this done?
here is my current code for the new company entry:
- <tr>
- <td valign="middle"><?php if (!$_GET["id"]) { ?>Industry<?php } ?></td>
- <td valign="middle"><?php if (!$_GET["id"]) { ?>:<?php } ?></td>
- <td valign="middle">
- <div id="container">
- <select id="cboIndustry" name="cboIndustry[]" style="width: 80%" <?php if (!$_GET["id"]) { ?>required<?php } ?>>
- <option value="" selected>[Industry..]</option>
- <?php
- $mysql_command = "CALL sp_populate_industry()";
- $mysql_query = $mysql_connection->prepare($mysql_command);
- $mysql_query->execute();
- while($mysql_row = $mysql_query->fetch())
- {
- ?>
- <option value="<?php echo $mysql_row['industry_id']; ?>"><?php echo $mysql_row['industry_name']; ?></option>
- <?php } ?>
- </select>
- <button id="btnAddIndustry" type="button" style="width:150px">Add More</button>
- </div>
- </td>
- </tr>
- <script type="text/javascript">
- $(function() {
- var counter = 0;
- $('#btnAddIndustry').click(function() {
- counter++;
- var newField = $('#cboIndustry').clone().
- attr({id: 'cboIndustry_' + counter,
- name: 'cboIndustry[]'});
- $('#container').append(newField).
- append('<button id="btnDeleteIndustry" type="button" class="delete">Delete</button>');
- });
- $('#container').on('click', 'button.delete', function()
- { $(this).prev().addBack().remove(); });
- });
- </script>