Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.75 KB | None | 0 0
  1. <div id="dialog-form" title="Add Vendor">
  2. <p class="validateTips">All form fields are required.</p>
  3.  
  4. <!-- Dialog box displayed after add row button is clicked -->
  5. <form>
  6. <fieldset>
  7. <label for="mr_name">Vendor</label>
  8. <input type="text" name="mr_name" id="mr_name" class="text ui-widget-content ui-corner-all" value="test">
  9. <label for="buyer_id">Buyer ID</label>
  10. <input type="text" name="buyer_id" id="buyer_id" class="text ui-widget-content ui-corner-all" value="99">
  11. <label for="poc_n">POC Name</label>
  12. <input type="text" name="poc_n" id="poc_n" class="text ui-widget-content ui-corner-all" value="name">
  13. <label for="poc_p">POC Email</label>
  14. <input type="text" name="poc_e" id="poc_e" class="text ui-widget-content ui-corner-all" value="test@tst.com">
  15. <label for="poc_p">POC Phone</label>
  16. <input type="text" name="poc_p" id="poc_p" class="text ui-widget-content ui-corner-all" value="5555555555">
  17.  
  18. <!-- Allow form submission with keyboard without duplicating the dialog button -->
  19. <input type="submit" id="submit" tabindex="-1" style="position:absolute; top:-1000px">
  20. </fieldset>
  21. </form>
  22. </div>
  23.  
  24.  
  25.  
  26. <div id="users-contain" class="ui-widget">
  27. <table id="html_master" class="ui-widget ui-widget-content">
  28. <thead>
  29. <tr class="ui-widget-header">
  30. <td>ID</td>
  31. <td>Vendor</td>
  32. <td>Buyer ID</td>
  33. <td>POC Name</td>
  34. <td>POC Email</td>
  35. <td>POC Phone</td>
  36. <td>Edit</td>
  37. </tr>
  38. </thead>
  39. <tbody>
  40.  
  41.  
  42. <?php
  43. /* Foreach loop that brings in information to populate table */
  44. foreach ($dbh->query($sql) as $rows){
  45. ?>
  46. <tr id="<?php echo intval ($rows['MR_ID'])?>">
  47. <td class="mr_id" id="<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo intval ($rows['MR_ID'])?></td>
  48. <td class="mr_name" id="mr_name-<?php echo intval ($rows['MR_ID'])?>" name="field" contenteditable="false"><?php echo $rows['MR_Name']?></td>
  49. <td class="buyer_id" id="buy_id<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['Buyer_ID']?></td>
  50. <td class="poc_n" id="poc_n-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_N']?></td>
  51. <td class="poc_e" id="poc_e-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_E']?></td>
  52. <td class="poc_p" id="poc_p-<?php echo intval ($rows['MR_ID'])?>" contenteditable="false"><?php echo $rows['MR_POC_P']?></td>
  53. <td><input type="button" class="edit" name="edit" value="Edit">
  54. </tr>
  55. <?php
  56. }
  57. ?>
  58. </tbody>
  59. </table>
  60.  
  61. // ----- Dialog Box for adding a row -----
  62.  
  63. $( function() {
  64.  
  65. var dialog, form,
  66.  
  67. emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/,
  68. phoneRegex = /^(?:(?:+?1s*(?:[.-]s*)?)?(?:(s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])s*)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))s*(?:[.-]s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})s*(?:[.-]s*)?([0-9]{4})(?:s*(?:#|x.?|ext.?|extension)s*(d+))?$/,
  69. mr_name = $( "#mr_name" ),
  70. buyer_id = $( "#buyer_id" ),
  71. poc_n = $( "#poc_n" ),
  72. poc_e = $( "#poc_e" ),
  73. poc_p = $( "#poc_p" ),
  74. allFields = $( [] ).add( mr_name ).add( buyer_id ).add( poc_n ).add( poc_e ).add( poc_p ),
  75. tips = $( ".validateTips" );
  76. console.log(allFields);
  77.  
  78. function updateTips( t ) {
  79. tips
  80. .text( t )
  81. .addClass( "ui-state-highlight" );
  82. setTimeout(function() {
  83. tips.removeClass( "ui-state-highlight", 1500 );
  84. }, 500 );
  85. }
  86.  
  87. function checkRegexp( o, regexp, n ) {
  88. if ( !( regexp.test( o.val() ) ) ) {
  89. o.addClass( "ui-state-error" );
  90. updateTips( n );
  91. return false;
  92. } else {
  93. return true;
  94. }
  95. }
  96.  
  97. function addVendor() {
  98. var valid = true;
  99. allFields.removeClass( "ui-state-error" );
  100. // ----- Validation for each input in add row dialog box -----
  101. valid = valid && checkRegexp( mr_name, /^[a-z]([0-9a-z_s])+$/i, "Please enter a valid vendor name" );
  102. valid = valid && checkRegexp( buyer_id, /^(0|[1-9][0-9]*)$/, "Please enter a valid Buyer ID" );
  103. valid = valid && checkRegexp( poc_n, /^[a-zA-Z ]*$/, "Please enter a valid name" );
  104. valid = valid && checkRegexp( poc_e, emailRegex, "Please enter a valid email" );
  105. valid = valid && checkRegexp( poc_p, phoneRegex, "Please enter a valid phone number" );
  106. console.log(allFields);
  107. if ( valid ) {
  108. var $tr = $( "#html_master tbody tr" ).eq(0).clone();
  109. var dict = {};
  110. var errors = "";
  111. $.each(allFields, function(){
  112. $tr.find('.' + $(this).attr('id')).html( $(this).val()+"-"+buyer_id );
  113. var type = $(this).attr('id');
  114. var value = $(this).val();
  115. console.log(type + " : " + value);
  116. // ----- Switch statement that provides validation for each table cell -----
  117. switch (type) {
  118. case "mr_id":
  119. dict["MR_ID"] = value;
  120. break;
  121. case "mr_name":
  122. dict["MR_Name"] = value;
  123. break;
  124. case "buyer_id":
  125. dict["Buyer_ID"] = value;
  126. break;
  127. case "poc_n":
  128. dict["MR_POC_N"] = value;
  129. break;
  130. case "poc_e":
  131. dict["MR_POC_E"] = value;
  132. break;
  133. case "poc_p":
  134. dict["MR_POC_P"] = value;
  135. break;
  136. }
  137. });
  138. console.log(dict);
  139. $tr.find('.mr_id').html( $( "#html_master tbody tr" ).length + 1 );
  140. $( "#html_master tbody" ).append($tr);
  141. dialog.dialog( "close" );
  142.  
  143.  
  144. var request = $.ajax({
  145. type: "POST",
  146. url: "insert-copy.php",
  147. data: dict
  148. });
  149.  
  150. request.done(function (response, textStatus, jqXHR){
  151. if(JSON.parse(response) == true){
  152. console.log("row inserted");
  153. } else {
  154. console.log("row failed to insert");
  155. console.log(response);
  156. }
  157. });
  158.  
  159. // Callback handler that will be called on failure
  160. request.fail(function (jqXHR, textStatus, errorThrown){
  161. console.error(
  162. "The following error occurred: "+
  163. textStatus, errorThrown
  164. );
  165. });
  166.  
  167. // Callback handler that will be called regardless
  168. // if the request failed or succeeded
  169. request.always(function () {
  170.  
  171. });
  172.  
  173.  
  174. }
  175. return valid;
  176. }
  177.  
  178. var dialog = $( "#dialog-form" ).dialog({
  179. autoOpen: false,
  180. height: 400,
  181. width: 350,
  182. modal: true,
  183. buttons: {
  184. "Add Row": addVendor,
  185. Cancel: function() {
  186. dialog.dialog( "close" );
  187. }
  188. },
  189. close: function() {
  190. form[ 0 ].reset();
  191. allFields.removeClass( "ui-state-error" );
  192. }
  193. });
  194.  
  195. form = dialog.find( "form" ).on( "submit", function( event ) {
  196. event.preventDefault();
  197. addVendor();
  198. });
  199.  
  200. $( ".create-user" ).button().on( "click", function() {
  201. dialog.dialog({
  202. position: ['center', 'top'],
  203. show: 'blind',
  204. hide: 'blind'
  205. });
  206. dialog.dialog("open");
  207. });
  208. });
  209.  
  210. function insertIntoTable() {
  211.  
  212. var tableName = document.getElementById("tableNameInput").value;
  213.  
  214. var dict = { tableName: tableName, mrName: 'Temp Object' };
  215.  
  216. request = $.ajax({
  217. type: "POST",
  218. url: "insert-copy.php",
  219. data: dict
  220. });
  221.  
  222. request.done(function (response, textStatus, jqXHR){
  223. if(JSON.parse(response) == true){
  224. console.log("row inserted");
  225. } else {
  226. console.log("row failed to insert");
  227. }
  228. });
  229.  
  230. // Callback handler that will be called on failure
  231. request.fail(function (jqXHR, textStatus, errorThrown){
  232. // Log the error to the console
  233. console.error(
  234. "The following error occurred: "+
  235. textStatus, errorThrown
  236. );
  237. });
  238.  
  239. // Callback handler that will be called regardless
  240. // if the request failed or succeeded
  241. request.always(function () {
  242.  
  243. });
  244.  
  245. }
  246.  
  247.  
  248.  
  249. <?php
  250.  
  251. $MR_ID = $_POST['MR_ID'];
  252. $MR_Name = $_POST['MR_Name'];
  253. $Buyer_ID = $_POST['Buyer_ID'];
  254. $MR_POC_N = $_POST['MR_POC_N'];
  255. $MR_POC_E = $_POST['MR_POC_E'];
  256. $MR_POC_P = $_POST['MR_POC_P'];
  257.  
  258. $host="xxxxxxxxxxx";
  259. $dbName="xxxxxx";
  260. $dbUser="xxxxxxxxxxxxxx";
  261. $dbPass="xxxxxxxxx";
  262.  
  263. $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);
  264.  
  265. $sql = "INSERT INTO Stage_Rebate_Master (MR_ID, MR_Name, Buyer_ID, MR_POC_N, MR_POC_E, MR_POC_P) VALUES (?, ?, ?, ?, ?, ?)";
  266. $stmt = $pdo->prepare($sql);
  267. $result = $stmt->execute(array($MR_ID, $MR_Name, $Buyer_ID, $MR_POC_N, $MR_POC_E, $MR_POC_P));
  268. echo json_encode($result);
  269.  
  270. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement