Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Title of the document</title>
  5.  
  6.  
  7. <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  8.  
  9. </head>
  10. <?php
  11. $hostname = 'localhost';
  12. $username = 'root';
  13. $password = '';
  14. $dbname = 'work_task';
  15.  
  16. $mysqli = new mysqli($hostname, $username, $password, $dbname);
  17.  
  18. if ($mysqli->connect_errno) {
  19. die("Error! Failed to connect to MySQL!");
  20. }
  21.  
  22. if($_GET){
  23. $fields = array();
  24. $query = $mysqli->prepare("SELECT * FROM brand_custom_fields WHERE brand_id = 45 ");
  25. $query->execute();
  26. $result = $query->get_result();
  27. if (!$result) {
  28. die('Error! Failed to process query');
  29. }
  30.  
  31. if ($result->num_rows > 0) {
  32. while ($row = $result->fetch_assoc()) {
  33. array_push($fields,$row);
  34. }
  35. print_r($fields);
  36. }
  37. else{
  38. echo "No products";
  39. }
  40. }
  41.  
  42.  
  43. ?>
  44. <body>
  45. <!--hi -->
  46. <div class="container">
  47. <form method="POST">
  48. <div class="control-group">
  49. <label class="control-label">Допълнителна информация</label>
  50.  
  51.  
  52. <input type="hidden" name="brand_id" value="45">
  53. <div class="prototype" style="display:none">
  54. <div class="fields">
  55. <label>Type of field</label>
  56. <select name="field_choice[]" id="" class="form-control field_choice">
  57. <option></option>
  58. <option value="0">Text</option>
  59. <option value="1">Link</option>
  60. </select>
  61.  
  62. <div class="text_field" id="" style="display:none">
  63. <label>Text: </label>
  64. <input type="text" name="text_fields[]" class="form-control">
  65. </div>
  66. <div class="link_field" id="" style="display:none">
  67. <label>Link desciprtion: </label>
  68. <input type="text" name="link_descr_field[]" class="form-control">
  69. <label>URL: </label>
  70. <input type="text" name="link_url[]" class="form-control">
  71. </div>
  72. <button type="button" class="remove"> x remove </button>
  73.  
  74. </div>
  75. </div>
  76. </div>
  77.  
  78. <div class="dynamic">
  79. <a href="javascript:;" class="new_custom_field">+ add </a>
  80.  
  81. </div>
  82. <input type="submit" name="Submit">
  83. </form>
  84. </div>
  85.  
  86.  
  87. <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  88. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  89. <script type="text/javascript">
  90.  
  91. // Adjusting ids to turn on hide and show when clicking select button
  92.  
  93. var i = 1;
  94. $(".new_custom_field").on("click",function() {
  95. var element = $( ".fields" ).first();
  96. var clone = element.clone();
  97. clone.appendTo( ".dynamic").insertBefore("a");
  98. clone.children("select").attr("id","field_choice_"+i);
  99. clone.children(".text_field").attr("id","text_field_"+i);
  100. clone.children(".link_field").attr("id","link_field_"+i);
  101. clone.children("#field_choice_"+i).change(select_change_handler);
  102.  
  103.  
  104. function select_change_handler(e) {
  105. var value = $(this).val();
  106. var id = $(this).attr('id');
  107. console.log(value + ' | ' + id);
  108. var actualId = id.replace("field_choice_","");
  109. parseInt(actualId);
  110. if(value == 0){
  111. $("#link_field_"+actualId).hide();
  112. $("#text_field_"+actualId).show();
  113.  
  114. }
  115. else{
  116. $("#text_field_"+actualId).hide();
  117. $("#link_field_"+actualId).show();
  118.  
  119. }
  120.  
  121.  
  122. }
  123. i++;
  124. });
  125.  
  126.  
  127.  
  128. </script>
  129. <script type="text/javascript">
  130. $(".remove").on("click",".remove",function(){
  131. console.log(1);
  132. //$(this).parent().delete();
  133. });
  134.  
  135. </script>
  136. </body>
  137. </html>
  138. <?php
  139. if($_POST){
  140. echo 1;
  141. $sql = "DELETE FROM brand_custom_fields WHERE brand_id = 45";
  142. if ($mysqli->query($sql) === TRUE) {
  143. echo "Sucecss";
  144. }
  145.  
  146. $data = $_POST;
  147. $brand_id = $data["brand_id"];
  148. $arrangedData = array();
  149. $num = 0;
  150.  
  151. // never take the first element
  152. $num = 0;
  153. for($i=1; $i <count($data["field_choice"]);$i++){
  154.  
  155. if($data["field_choice"][$i]==0){
  156. if(!empty($data["text_fields"][$i])){
  157. $arrangedData[$num]["field_choice"]=0;
  158. $arrangedData[$num]["text_field"]=$data["text_fields"][$i];
  159. $text = $arrangedData[$num]["text_field"];
  160.  
  161. //Insert заявка
  162.  
  163. $sql="INSERT INTO brand_custom_fields (brand_id, custom_field_type, custom_field_text) VALUES ('$brand_id',0,'$text')";
  164.  
  165. if ($mysqli->query($sql) === TRUE) {
  166. echo 1;
  167. }
  168. }
  169. }
  170. else{
  171. if(!empty($data["link_descr_field"][$i])&& !empty($data["link_url"][$i])){
  172. $arrangedData[$num]["field_choice"]=1;
  173. $arrangedData[$num]["link_description"]=$data["link_descr_field"][$i];
  174. $arrangedData[$num]["link_url"]=$data["link_url"][$i];
  175. $desc =$data["link_descr_field"][$i];
  176. $url = $data["link_url"][$i];
  177. //Insert заявка
  178. $sql="INSERT INTO brand_custom_fields (brand_id, custom_field_type, custom_field_link_descr,custom_field_url) VALUES ('$brand_id',1,'$desc','$url')";
  179.  
  180. if ($mysqli->query($sql) === TRUE) {
  181. echo 1;
  182. }
  183.  
  184. }
  185.  
  186. }
  187. $num++;
  188. }
  189. print_r($arrangedData);
  190.  
  191. }
  192.  
  193. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement