title: How can I create the callback in ajax form?
body: <p>How can I create the callback after a file is upload on the server?</p>
  
  <p>I want to upload a file on a server after a JS function call.
  I want to call the function <code>addDownload</code> and after this function is complete, then call the next javascript function. How can I do this?</p>
  
  <p>I have a following source code:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;form id="imageform" method="post" enctype="multipart/form-data" action="actions/downloadsadd.php"&gt;
   &lt;strong&gt;File: &lt;/strong&gt;&lt;input name="photoimg" id="photoimg" style="width: 100px;" type="file" /&gt;
  Select Image: &lt;br /&gt;
  &lt;div id="divPreview"&gt;&lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>addDownload: function ()
      {
  
          $("#divPreview").html('');
          $("#divPreview").html('&lt;img src="Images/loader.gif" alt="Uploading...."/&gt;');
          $("#imageform").ajaxForm(
          {
              target: '#divPreview'
          }).submit();             
      },
  </code></pre>
  
  <p>PHP - downloads.php:</p>
  
  <pre><code>public function addDownloads() {
  
              $db = new DB();
              $db-&gt;connect();
  
              $path = "../../images/upload/files/";
  
              $valid_formats = array("php", "phtml", "php3", "php4", "js", "shtml", "pl", "py", "html", "exe", "bat", "htm", "sql");
              if (isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST") {
                  $name = $_FILES['photoimg']['name'];
                  $size = $_FILES['photoimg']['size'];
                  if (strlen($name)) {
                      list($txt, $ext) = explode(".", $name);
                      if (!in_array($ext, $valid_formats)) {
                          if ($size &lt; (1024 * 1024)) { // Image size max 1 MB
                              $actual_image_name = time() . "." . $ext;
                              $tmp = $_FILES['photoimg']['tmp_name'];
                              if (move_uploaded_file($tmp, $path . $actual_image_name)) {
  
  
                                  $arr = array(
                                      'file' =&gt; $actual_image_name
                                  );
  
  
                                  dibi::query('INSERT INTO [downloads]', $arr);
  
                                  echo "FILE:" .$actual_image_name;
                              }
                              else
                                  echo "Failed upload!";
                          }
                          else
                              echo "Image file size max 1 MB";
                      }
                      else
                          echo "Invalid file format..";
                  }
                  else
                      echo "Please select image..!";
              }
          }
  </code></pre>