title: Submit form with text and images using PHP and Ajax
body: <p>I have an <code>HTML form</code> with some text input and some file inputs. I am trying to upload the text and images to the database using ajax to execute it. I have tried solutions <a href="https://stackoverflow.com/questions/30161862/text-content-and-fileupload-with-ajax">here</a>  and <a href="https://stackoverflow.com/questions/21164365/how-to-send-image-to-php-file-using-ajax">here</a> btut neither works for me. Below are my codes</p>
  
  <p><strong>HTML form</strong></p>
  
  <pre><code>&lt;form action="genformexec.php" name="genreg" id="genreg" method="post" enctype="multipart/form-data"&gt;
        &lt;label class="type" for="ccode"&gt;*Your confirmation code is the series of numbers sent to your mail after payment verification&lt;/label&gt;
        &lt;input class="half" type="text" name="ccode" id="ccode" placeholder="Confirmation Code"&gt;
        &lt;input class="half" type="email" name="uemail" id="uemail" placeholder="Email Address"&gt;
        &lt;input class="half" type="text" name="fname" id="fname" placeholder="First Name"&gt;
        &lt;input class="half" type="text" name="lname" id="lname" placeholder="Last Name"&gt;
        &lt;input class="full" type="text" name="cname" id="cname" placeholder="Company Name"&gt;
        &lt;input class="full" type="text" name="crcnum" id="crcnum" placeholder="Company RC/BN Number"&gt;
        &lt;input class="full" type="text" name="uaddress" id="uaddress" placeholder="Residential Address"&gt;
        &lt;input class="full" type="text" name="caddress" id="caddress" placeholder="Company Address"&gt;
        &lt;textarea class="full" rows="7" name="briefdes" id="briefdes" placeholder="Brief description about your company"&gt;&lt;/textarea&gt;
        &lt;input class="half" type="text" name="uname" id="uname" placeholder="Username"&gt;
        &lt;input class="half" type="text" name="uphone" id="uphone" placeholder="Phone Number"&gt;
        &lt;input class="half" type="text" name="pword" id="pword" placeholder="Password"&gt;
        &lt;input class="half" type="text" name="cfpword" id="cfpword" placeholder="Re-Type Password"&gt;
        &lt;div class="profImage"&gt;&lt;img id="logo_prev" src=""&gt;&lt;/div&gt;&lt;br&gt;
        &lt;label for="profimages"&gt;Upload Company Logo/Profile Picture&lt;/label&gt;&lt;br&gt;&lt;br&gt;
        &lt;input type="file" onchange="logo_prev()" name="profimages" id="profimages"&gt;
        &lt;div class="coverimage"&gt;&lt;img id="cover_prev"src=""&gt;&lt;/div&gt;
        &lt;label for="coverimage"&gt;Upload a cover image for your profile&lt;/label&gt;&lt;br&gt;&lt;br&gt;
        &lt;input type="file" onchange="cover_prev()" name="coverimage" id="coverimage"&gt;
        &lt;input type="file" name="profimages" id="profimages"&gt;
        &lt;label class="type"&gt;Receive request about&lt;/label&gt;
        &lt;div class="postLimit"&gt;
        &lt;p class="limitType"&gt;Food Commodities&lt;/p&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Fresh Yam"&gt;Fresh Yam&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Garri"&gt;Garri&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Banana and Plantain"&gt;Banana and Plantain&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Plantain Flour"&gt;Plantain Flour&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Cassava Flour"&gt;Cassava Flour&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Chilli Pepper"&gt;Chilli Pepper&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Kola Nut"&gt;Kola Nut&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Palm Oil"&gt;Palm Oil&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Smoked Fish"&gt;Smoked Fish&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Dried Vegetable"&gt;Dried Vegetable&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Cray Fish"&gt;Cray Fish&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Shea Butter"&gt;Shea Butter&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Cassava Chips"&gt;Cassava Chips&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Fresh Onions"&gt;Fresh Onions&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Curry Leaves"&gt;Curry Leaves&lt;/div&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Bitter Kola"&gt;Bitter Kola&lt;/div&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
        &lt;div class="checkin"&gt;&lt;input type="checkbox" name="postValue[]" value="Bitter Kola, Fresh Yam, Garri, Banana and Plantain, Plantain Flour, Cassava Flour, Chilli Pepper, Kola Nut, Palm Oil, Smoked Fish, Dried Vegatables, Cray Fish, Shea Butter, Cassava Chips, Fresh Onions, Curry Leaves"&gt;All&lt;/div&gt;
        &lt;/div&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;
        &lt;label class="type" for="regas"&gt;You are registering as:&lt;/label&gt;
        &lt;input type="text" class="full readonly" readonly name="regas" id="regas" value="Exporter"&gt;
        &lt;label class="type" for="package"&gt;Package type:&lt;/label&gt;
        &lt;input type="text" class="full readonly" readonly name="package" id="package" value="Bronze"&gt;
        &lt;input type="submit" name="regSubmit" id="regSubmit" value="Submit"&gt;
    &lt;/form&gt;
  </code></pre>
  
  <p><strong>Ajax Request</strong></p>
  
  <pre><code>$("#genreg").submit(function(e) {
      var formData = new FormData($(this)[0]);
      $.ajax({
          url: "genformexec.php",
          type: "POST",
          data: formData,
          async: false,
          success: function (msg) {
              alert(msg)
          },
          cache: false,
          contentType: false,
          processData: false
      });
      e.preventDefault();
  });
  </code></pre>
  
  <p><strong>PHP Script</strong></p>
  
  <pre><code>&lt;?php
  error_reporting(0);
  session_start();
  require_once ("db.php");
  $db = new MyDB();
  
  if (isset($_POST['regSubmit']))
  {
    $fname = strip_tags(@$_POST['fname']);
    $lname = strip_tags(@$_POST['lname']);
    $cname = strip_tags(@$_POST['cname']);
    $crnum = strip_tags(@$_POST['crcnum']);
    $uaddress = strip_tags(@$_POST['uaddress']);
    $caddress = strip_tags(@$_POST['caddress']);
    $briefdes = strip_tags(@$_POST['briefdes']);
    $uname = strip_tags(@$_POST['uname']);
    $uphone = strip_tags(@$_POST['uphone']);
    $pword = $_POST['pword'];
    $cfpword = $_POST['cfpword'];
    $uemail = $_POST['uemail'];
    $regas = strip_tags(@$_POST['regas']);
    $package = strip_tags(@$_POST['package']);
    $imagedir = './profimages/';
    $profimages = $_FILES['profimages']['name'];
    $profimagestmpname = $_FILES['profimages']['tmp_name'];
    $profimages = $_FILES['profimages']['type'];
    $profimages = $_FILES['profimages']['size'];
    $regdate = date('d/m/Y');
    $ccode = strip_tags(@$_POST['ccode']);
    $posttype = join(", ", $_POST['postValue']);
    $status = "0";
    $verified = "0";
    $zero = "0";
    $website = strip_tags(@$_POST['website']);
    $coverimage = $_FILES['coverimage']['name'];
    $coverimagetmpname = $_FILES['coverimage']['tmp_name'];
    $coverimage = $_FILES['coverimage']['type'];
    $coverimage = $_FILES['coverimage']['size'];
  
    $csql = "SELECT COUNT(*) as count FROM paymentverification WHERE code = $ccode";
  
    $psql = $db-&gt;prepare("SELECT * FROM paymentverification WHERE code = ?");
  
    $psql-&gt;bindParam(1, $ccode, SQLITE3_TEXT);
  
    $cret = $db-&gt;querySingle($csql);
    $pret = $psql-&gt;execute();
  
    if ($cret == 1)
    {
      while ($prow = $pret-&gt;fetchArray(SQLITE3_ASSOC))
      {
          $cpackage = $prow['package'];
  
    if ($cpackage == $package)
    {
  
    $cql = &lt;&lt;&lt;EOF
    SELECT COUNT(*) FROM users WHERE ccode = $ccode;
  EOF;
  
  $count = $db-&gt;querySingle($cql);
  
  if ($count == 1)
  {
    echo "This code as already been used";
  }
  else
  {
      if ($pword != $cfpword)
      {
        echo 'The passwords you entered do not match. Please try again!.';
      }
      else
      {
          if (strlen($pword) &lt;= 8)
          {
              echo 'Your password must be more than 8 characters';
          }
          else
          {
  
  
                  $imagepath = $imagedir . $profimages;
                  $imagepath_2 = $imagedir . $coverimage;
  
                  $imageresult = move_uploaded_file($profimagestmpname, $imagepath);
                  $imageresult_2 = move_uploaded_file($coverimagetmpname, $imagepath_2);
  
                  if (!$imageresult &amp;&amp; !$imageresult_2)
                  {
                    echo "Error uploading Company Logo/Profile Picture";
                  }
                  if (!get_magic_quotes_gpc())
                  {
                    $profimages = addslashes($profimages);
                    $imagepath = addslashes($imagepath);
                    $coverimage = addslashes($coverimage);
                    $imagepath_2 = addslashes($imagepath_2);
                  }
  
                  $stmt = $db-&gt;prepare('INSERT INTO users (fname, lname, cname, crcnum, caddress, uaddress, briefdes, uemail, uphone, uname, regas, pword, cfpword, profimages, package, regdate, ccode, posttype, status, verified, website, coverimage)
                  VALUES (:fname, :lname, :cname, :crnum, :caddress, :uaddress, :briefdes, :uemail, :uphone, :uname, :regas, :pword, :cfpword, :profimages, :package, :regdate, :ccode, :posttype, :status, :verified, :website, :coverimage)');
  
                  $stmt-&gt;bindValue(':fname', $fname, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':lname', $lname, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':cname', $cname, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':crnum', $crnum, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':caddress', $caddress, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':uaddress', $uaddress, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':briefdes', $briefdes, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':uemail', $uemail, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':uphone', $uphone, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':regas', $regas, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':regdate', $regdate, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':uname', $uname, SQLITE3_TEXT);
                  //Come back to password_hash password $stmt-&gt;bindValue(':pword', password_hash($pword, PASSWORD_BCRYPT));
                  $stmt-&gt;bindValue(':pword', password_hash($pword, PASSWORD_BCRYPT));
                  $stmt-&gt;bindValue(':cfpword', password_hash($cfpword, PASSWORD_BCRYPT));
                  $stmt-&gt;bindValue(':profimages', $imagepath, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':package', $package, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':ccode', $ccode, SQLITE3_INTEGER);
                  $stmt-&gt;bindValue(':posttype', $posttype, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':status', $status, SQLITE3_INTEGER);
                  $stmt-&gt;bindValue(':verified', $verified, SQLITE3_INTEGER);
                  $stmt-&gt;bindValue(':website', $website, SQLITE3_TEXT);
                  $stmt-&gt;bindValue(':coverimage', $imagepath_2, SQLITE3_TEXT);
  
                  $result = $stmt-&gt;execute();
  
                  if ($result)
                  {
                      echo "Account Successfully Created";
                  }
                  else
                  {
                      echo "Sorry!.....There was an issue creating your account. Please try again";
                  }
            }
        }
      }
    }
      else
      {
          echo "You payed for the $cpackage package";
          exit();
          }
        }
    }
  }
  ?&gt;
  </code></pre>
  
  <p>So please what is the issue with any of mu codes and how do i fix and make this work. Sorry if i post unnecessary codes (I just thought they are all need for you to help me). Thanks </p>