Guest User

Untitled

a guest
Nov 18th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. {!! Form::open(['action'=>'AdminPromotionsController@store', 'method' => 'POST','enctype'=>'multipart/form-data']) !!}
  2. <div class="form-group">
  3. <form name="add_name" id="add_name">
  4. <div class="table-responsive">
  5. <table class="table table-bordered" id="dynamic_field">
  6. <tr>
  7. <td> {{Form::file('promotion_image[]')}}</td>
  8.  
  9. <td>{{ Form::button('Add', ['class' => 'btn btn-success', 'id'=>'add','name'=>'add']) }}</td>
  10. </tr>
  11. </table>
  12. {{Form::submit('submit', ['class'=>'btn btn-primary'])}}
  13. </div>
  14. </form>
  15. </div>
  16. {!! Form::close() !!}
  17.  
  18. $this->validate($request, [
  19. 'promotion_image' => 'required'
  20. ]);
  21.  
  22. //Handle File Upload
  23. if($request->hasFile('promotion_image[]')){
  24. // Get FileName
  25. $filenameWithExt = $request->file('promotion_image[]')->getClientOriginalName();
  26. //Get just filename
  27. $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
  28. //Get just extension
  29. $extension = $request->file('promotion_image[]')->getClientOriginalExtension();
  30. //Filename to Store
  31. $fileNameToStore = $filename.'_'.time().'.'.$extension;
  32. //Upload Image
  33. $path = $request->file('promotion_image[]')->storeAs('public/promotion_images',$fileNameToStore);
  34. }else{
  35. $fileNameToStore='noimage.jpg';
  36. }
  37.  
  38. $promotion = new Promotion;
  39. $promotion->promotion_image = $fileNameToStore;
  40. $promotion->save();
  41.  
  42. <script>
  43. $(document).ready(function(){
  44. var i=1;
  45. $('#add').click(function(){
  46. i++;
  47. $('#dynamic_field').append('<tr id="row'+i+'"><td>{{Form::file('promotion_image[]',['class'=>'form-control name_list'])}}</td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
  48. });
  49. $(document).on('click', '.btn_remove', function(){
  50. var button_id = $(this).attr("id");
  51. $('#row'+button_id+'').remove();
  52. });
  53. $('#submit').click(function(){
  54. $.ajax({
  55. url:"name.php",
  56. method:"POST",
  57. data:$('#add_name').serialize(),
  58. success:function(data)
  59. {
  60. alert(data);
  61. $('#add_name')[0].reset();
  62. }
  63. });
  64. });
  65. });
  66. </script>
  67.  
  68. $('#add_name').serialize() using this you can't get image object in post using ajax.
  69.  
  70. var form = $('form')[0];
  71. var formData = new FormData(form);
Add Comment
Please, Sign In to add comment