Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [JavaScript]Wysyłanie formularza ze zdjęciem bez przeładowania strony
trifek
post 29.10.2019, 15:45:48
Post #1





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

Ostrzeżenie: (0%)
-----


Witajcie,

Mam małe problemy z JS. Mam taki kod:

  1.  
  2. <div class="container">
  3. <div class="row sortableImages">
  4. <div class="col-sm-2 imgUp imagesViewBox">
  5. <form action="server.php" method="post" id="form1">
  6. <div class="imagePreview"></div>
  7. <label class="btn btn-primary">
  8. Upload 1
  9. <input type="file" accept="image/jpeg" class="uploadFile img" value=""
  10. style="width: 0px;height: 0px;overflow: hidden;" name="file1" id="file1"><i
  11. class="fa fa-times del" data-id="delete-1"></i>
  12. </label>
  13. </form>
  14. </div>
  15. <div class="col-sm-2 imgUp imagesViewBox">
  16. <form action="server.php" method="post" id="form2">
  17. <div class="imagePreview"></div>
  18. <label class="btn btn-primary">
  19. Upload 2
  20. <input type="file" accept="image/jpeg" class="uploadFile img" value=""
  21. style="width: 0px;height: 0px;overflow: hidden;" name="file2" id="file2"><i
  22. class="fa fa-times del" data-id="delete-2"></i>
  23. </label>
  24. </form>
  25. </div>
  26. <div class="col-sm-2 imgUp imagesViewBox">
  27. <form action="server.php" method="post" id="form3">
  28. <div class="imagePreview"></div>
  29. <label class="btn btn-primary">
  30. Upload 3
  31. <input type="file" accept="image/jpeg" class="uploadFile img" value=""
  32. style="width: 0px;height: 0px;overflow: hidden;" name="file2" id="file3"><i
  33. class="fa fa-times del" data-id="delete-3"></i>
  34. </label>
  35. </form>
  36. </div>
  37. <div class="col-sm-2 imgUp imagesViewBox">
  38. <form action="server.php" method="post" id="form4">
  39. <div class="imagePreview"></div>
  40. <label class="btn btn-primary">
  41. Upload 4
  42. <input type="file" accept="image/jpeg" class="uploadFile img" value=""
  43. style="width: 0px;height: 0px;overflow: hidden;" name="file2" id="file4"><i
  44. class="fa fa-times del" data-id="delete-4"></i>
  45. </label>
  46. </form>
  47. </div>
  48. </div>
  49. </div>
  50.  
  51.  
  52. </body>
  53. </html>
  54. $(document).on("click", "i.del", function () {
  55. alert('delete file id =' + $(this).data("id"));
  56. $.ajax({
  57. url: 'deleteFile.php',
  58. dataType: 'text',
  59. cache: false,
  60. contentType: false,
  61. type: 'post',
  62. success: function (data) {
  63. alert(data);
  64. }
  65. });
  66. });
  67. $(function () {
  68. $(document).on("change", ".uploadFile", function () {
  69. var uploadFile = $(this);
  70. var files = !!this.files ? this.files : [];
  71. if (!files.length || !window.FileReader) return;
  72.  
  73. if (/^image/.test(files[0].type)) { // only jpg file
  74. var reader = new FileReader();
  75. reader.readAsDataURL(files[0]);
  76.  
  77. reader.onloadend = function () {
  78. //alert(uploadFile.closest(".upimage").find('.imagePreview').length);
  79. uploadFile.closest(".imgUp").find('.imagePreview').css("background-image", "url(" + this.result + ")");
  80. //alert('send form');
  81.  
  82. var file_data = $('#file1').prop('files')[0];
  83. var form_data = new FormData();
  84. form_data.append('file', file_data);
  85. $.ajax({
  86. url: 'upload.php',
  87. dataType: 'text',
  88. cache: false,
  89. contentType: false,
  90. processData: true,
  91. data: form_data,
  92. type: 'post',
  93. success: function (data) {
  94. alert(data);
  95. }
  96. });
  97. }
  98. }
  99. });
  100.  
  101.  
  102. $(".sortableImages").sortable({
  103. items: '.imagesViewBox',
  104. cursor: 'move',
  105. opacity: 0.5,
  106. containment: '.sortableImages',
  107. distance: 20,
  108. tolerance: 'pointer',
  109. update: function (event, ui) {
  110. $(".sortableImages .imagesViewBox").each(function (index) {
  111. index = index + 1;
  112. $.ajax({
  113. headers: {
  114. 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
  115. },
  116. url: 'savePosition.php&id=1111111&index=' + index + '&file=' + $(this).text(),
  117. dataType: 'text',
  118. type: 'post',
  119. cache: false,
  120. data: $(this).serialize(),
  121. success: function (data, textStatus, jQxhr) {
  122. },
  123. error: function (jqXhr, textStatus, errorThrown) {
  124. alert('Nastąpił problem z JSON. Proszę o kontakt z administratorem serwisu.');
  125. }
  126. });
  127. });
  128. }
  129. });
  130.  
  131. });
  132. </script>
  133. .imagePreview {
  134. width: 100%;
  135. height: 180px;
  136. background-position: center center;
  137. background: url(http://cliquecities.com/assets/no-image-e3699ae23f866f6cbdf8ba2443ee5c4e.jpg);
  138. background-color: #fff;
  139. background-size: cover;
  140. background-repeat: no-repeat;
  141. display: inline-block;
  142. box-shadow: 0px -3px 6px 2px rgba(0, 0, 0, 0.2);
  143. }
  144.  
  145. .btn-primary {
  146. display: block;
  147. border-radius: 0px;
  148. box-shadow: 0px 4px 6px 2px rgba(0, 0, 0, 0.2);
  149. margin-top: -5px;
  150. }
  151.  
  152. .del {
  153. position: absolute;
  154. top: 0px;
  155. right: 15px;
  156. width: 30px;
  157. height: 30px;
  158. text-align: center;
  159. line-height: 30px;
  160. background-color: rgba(255, 255, 255, 0.6);
  161. cursor: pointer;
  162. }
  163.  
  164. </style>


Podgląd: http://serwer1356363.home.pl/pub/kolor1.html




Po dodaniu zdjęcia w formularzu chciałbym go wysłać w "tle" bez przeładowania strony. W jaki sposób można to zrobić?
Wyskakuje mi błąd: TypeError: Can only call FormData.append on instances of FormData

Bardzo proszę o pomoc smile.gif

Ten post edytował trifek 29.10.2019, 15:56:52
Go to the top of the page
+Quote Post
rad11
post 29.10.2019, 18:26:35
Post #2





Grupa: Zarejestrowani
Postów: 1 270
Pomógł: 184
Dołączył: 7.10.2012
Skąd: Warszawa

Ostrzeżenie: (0%)
-----


Spróbuj ustawić processData na false
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 25.04.2024 - 03:32