jquery and upload a file

jquery and upload a file

I have this form:

  1. <form id="uploadform" name="uploadform" enctype="multipart/form-data">
  2. <p class="short">Email Address <small>(required)</small></p>
  3. <input type="text" name="email" id="email" placeholder="eg: me@dennisaucoin.com" tabindex="1" class="short" /><br />
  4. <p class="short">Amount Due <small>(required)</small></p>
  5. <input type="text" name="amount" id="amount" placeholder="eg: $500.00" tabindex="2" class="short" /><br />
  6. <p class="short">Due In <small>(required)</small></p>
  7. <input type="radio" id="due30" name="duein" value="30" class="duein" />30 Days &nbsp; <input type="radio" id="due45" name="duein" value="45" class="duein" />45 Days &nbsp; <input type="radio" id="due60" name="duein" value="60" class="duein" />60 Days &nbsp; <input type="radio" id="duexx" name="duein" value="xx" class="duein" /><input type="text" name="duenum" id="duenum" value="" style="width:25px;" disabled="true" /> Days<br />
  8. <p class="short">File <small>(required)</small></p>
  9. <input type="file" name="file" id="file"  tabindex="2" class="short" /><br />
  10. <p class="short">Status <small>(required)</small></p>
  11. <input type="radio" id="unpaid" name="status" value="unpaid" />Unpaid &nbsp; <input type="radio" id="paid" name="status" value="paid" />Paid<br />
  12. <p class="short">Notes <small>(hidden)</small></p>
  13. <textarea name="notes" id="notes" tabindex="4" class="short"></textarea><br />
  14. <input type="submit" value="Upload Invoice" id="submit" tabindex="7" class="short" /> <input type="reset" value="Reset" id="reset" tabindex="8" class="short" />   
  15. </form>
I can get it to validate the form correctly, but I can not seem to get the file upload / checked passed to the PHP file once you click the link.

The JQUERY code is:

  1. $('#uploadform').submit(function() {
  2. var email = $('#uploadform input:eq(0)').val();
  3. var amount = $('#uploadform input:eq(1)').val();
  4. var duein = $('input[name=duein]:checked', '#uploadform').val();
  5. var duenum = $('#uploadform #duenum').val();
  6. var file = $('input:file').val();
  7. var extension = file.replace(/^.*\./, '');
  8. var status = $('input[name=status]:checked', '#uploadform').val();
  9. var dataString = 'email=' + email + '&amount=' + amount + '&duein=' + duein + '&duenum=' + duenum + '&file=' + file + '&status=' + status;
  10. if(!isValidEmailAddress( email ) || amount=='' || (duein=='' || (duein=='xx' && duenum=='')) || file=='' || status=='') {
  11. $('#alert span').html('Please fill in the required fields.<br /><strong>Please try again.</strong>');
  12. $('#alert').fadeIn().click(function() {
  13. $(this).fadeOut();
  14. });
  15. } else if($.inArray(extension, ['pdf']) == -1) {
  16. $('#alert span').html('Please make sure you are upload a <strong>PDF file</strong>.<br /><strong>Please try again.</strong>');
  17. $('#alert').fadeIn().click(function() {
  18. $(this).fadeOut();
  19. });
  20. } else {
  21. $.ajax({
  22. type: 'POST',
  23. url: 'http://www.dennisaucoin.com/admin/upload.php',
  24. data: dataString,
  25. success: function(x){
  26. $('#alert span').html(x);
  27. $('#alert').fadeIn().click(function() {
  28. $(this).fadeOut();
  29. $('#email').val("");
  30. $('#amount').val("");
  31. $('#file').val("");
  32. $('#duenum').val("");
  33. $("#duenum").attr("disabled", true);
  34. $("input:radio").removeAttr("checked");
  35. });
  36. }
  37. });
  38. }
  39. return false;
  40. }); 
And I am using the following for the PHP code:

  1. <?php
  2. include("somefile.php");

  3. $email = $_POST['email'];
  4. $amount = $_POST['amount'];
  5. $duein = $_POST['status'];
  6. $duenum = $_POST['duenum'];
  7. $status = $_POST['status'];
  8. $file = $_FILES['file'];
  9. $notes = $_POST['notes'];

  10. $temp = explode(".", $_FILES['file']);

  11. if($email == "" OR $amount == "" OR $duein == "" OR ($duein == "xx" AND $duenum == "") OR $status == "") {
  12.   echo 'Please fill in the required fields. ('.$_GET['file'].')<br /><strong>Please try again.</strong>';
  13. } elseif($temp[1] != "pdf") {
  14.   echo 'Please make sure you are upload a <strong>PDF file</strong>. ('.$_FILES[file].') <br /><strong>Please try again.</strong>';
  15. } else {
  16.   $invoice = rand_string(15);
  17.   if (!move_uploaded_file($file, "../pdfs/".$invoice.".pdf")) {
  18.     echo 'There was an error while trying to upload your file.('.$_FILES[file].')<br /><strong>Please try again.</strong>';
  19.   } else {
  20.     $id = rand_string(15);
  21. if($duein == "xx") {
  22.   $duedate = mktime(0, 0, 0, date("m"), date("d")+$duenum, date("Y"));;
  23. } else {
  24.   $duedate = mktime(0, 0, 0, date("m"), date("d")+$duein, date("Y"));;
  25. }
  26.     mysql_query("INSERT INTO `sometable` (`id`, `email`, `invoice`, `email`, `amount`, duein`, `status`, `notes`, `created`, `updated`) VALUES ('$id', '$type', '$invoice', '$email', '$amount', '$duedate', '$status', '$notes', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())");
  27.     echo 'Your invoice has been upload to the website and the user has been notified via email.<br /><strong>You can either enter a new user or go back to the main page.</strong>';
  28.   }
  29. }
  30. ?> 

If anyone can help me out, it would be much appreciated. I am kinda in a pickle right now because I can make this part of my website live without this feature working.

Thank you,
Dennis

Thanks for all your help. If you every need anything PM me back.