Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropzone.js inside another PHP form

Despite there's some documentation, there are very few real examples on how to use it.

For example: I want to include Dropzone inside my existing form that adds a product with title, description and photos.

Dropzone itself works as a form. It would be nice to use Dropzone just to preview and validate image type and size, but let the main form submit all the data. How should I do it?

This would be the idea (however, I shouldn't include a form inside a form):

<form action="products/add.php" method="POST" class="form-horizontal" role="form">
    <div class="form-group">
      <legend>Add product</legend>
    </div>

    <label for="title">Title</label>
    <input type="text" name="title" id="input-title" class="form-control">

    <label for="description">Description</label>
    <input type="text" name="description" id="input-description" class="form-control">

    <form action="/file-upload" class="dropzone" id>
      <div class="dropzone-previews"></div>
    </form>

    <div class="form-group">
      <div class="col-sm-10 col-sm-offset-2">
        <button type="submit" class="btn btn-primary">Submit</button>
      </div>
    </div>
</form>
like image 972
whitenoisedb Avatar asked Jun 12 '15 17:06

whitenoisedb


1 Answers

Please try the following. You have to "load" dropzone programmatically as the creator explains here

	Dropzone.autoDiscover = false;
	jQuery(document).ready(function() {

	  $("div#my-awesome-dropzone").dropzone({
	    url: "/file/post"
	  });

	});
<link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.css" rel="stylesheet" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/min/basic.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.js"></script>


<form action="products/add.php" method="POST" class="form-horizontal" role="form">
  <div class="form-group">
    <legend>Add product</legend>
  </div>

  <label for="title">Title</label>
  <input type="text" name="title" id="input-title" class="form-control">

  <label for="description">Description</label>
  <input type="text" name="description" id="input-description" class="form-control">

  <div class="dropzone dropzone-previews" id="my-awesome-dropzone"></div>

  <div class="form-group">
    <div class="col-sm-10 col-sm-offset-2">
      <button type="submit" class="btn btn-primary">Submit</button>
    </div>
  </div>
</form>
like image 75
iWiFi Avatar answered Sep 30 '22 14:09

iWiFi