0

I want to upload different text files and process it with different php script based on the type of the machine that the text file came from.

$(document).ready(function(e){
$('#txtUpload').on('submit',function(e){
  var mType = $('#cmbmachine option:seleted').val();
  var page;

   if(mType == 'machine1')
   {
     page = 'machine1.php';
   }
   if(mType == 'machine2')
   {
     alert('To be added');
   }
  e.preventDefault();
  $.ajax({
     url: page,
     dataType: 'text',
     type: 'post',
     data: new FormData(this),
     contentType: false,
     cache: false,
     processData: false,
     success: function(data){
        alert('ok');
     }
  });
});
});

This is my code reference that I searched in http://www.formget.com/ajax-image-upload-php/

Any kind of help will be appreciated and thank you in advance.

Here is the html part:

<fieldset>
<legend><strong>Select type of machine model</strong></legend>
<select id="cmbmachine" name="machine" class="field">
<option value="machine1">Machine 1</option>
<option value="machine2">Machine 2</option>
</select>
</fieldset>
<fieldset>
    <legend><strong>Select a file to upload</strong></legend>
    <form id="txtUpload" method="post" enctype="multipart/form-data">
    <input type="file" name="files[]" size="40" multiple="multiple" />
     <br />
      <p></p>
       <input type="submit" value="Upload File" onclick="myFunction()">
       <br />
        <br />
    </form>

</fieldset>
<fieldset>
<legend><strong>Uploaded Files</strong></legend>
    <div id="uploaded"></div>
</fieldset>
<script type="text/javascript">
document.getElementById("cmbmachine").value="<?php echo $_GET['machine'];?>";
</script>

And here is the php part:

include 'functions.php';

set_time_limit(0);
if(isset($_FILES['files']))
{
foreach($_FILES['files']['tmp_name'] as$key => $tmp_name)
    {
        $file_name = $key.$_FILES['files']['name'][$key];
        $file_size = $_FILES['files']['size'][$key];
        $file_tmp = $_FILES['files']['tmp_name'][$key];
        $file_type = $_FILES['files']['type'][$key];

        if($file_size > 10000000) //10mb
        {
            echo "<script>alert('File exceeds file size')</script>";
        }

        if($file_type == "text/plain")
        {
            $i = 0;
             $file = fopen($file_tmp,"r");

             while(($data = fgetcsv($file, 1000, "\t"))!=FALSE)
             {
                if($i > 0)
                {
                    $data[0] = "";
                    $data[1] = "";
                    $data[3] = "";
                    $data[4] = "";
                    $data[5] = "";

                    unset($data[0],$data[1],$data[3],$data[4],$data[5]);
                     $line[] = $data;
                }
                $i++;
             }
             fclose($file);
             $j = 0;
             foreach($line as $value)
             {
                $newline = explode(" ",$value[6]);
                 $date  = trim($newline[0]);
                  $time = trim($newline[2]);
                   $newtime = date("H:i",strtotime($time));

                 try{
                    $query = $con->prepare("INSERT IGNORE INTO temp_logs(EmpID, ValidDate, ValidTime)VALUES(:id,:ddate,:time)");
                     $query->bindParam(':id',$value[2]);
                      $query->bindParam(':ddate',$date);
                       $query->bindParam(':time',$time);
                        $query->execute();
                 }
                 catch(PDOException $e){
                    echo $e->getMessage();
                     exit;
                 }
                 $j++;
                 echo $j . " row(s) processed.";

                 echo str_repeat(' ',1024 * 64);

                 flush();

                 sleep(0);
             }
             if($query)
             {
                echo "Process completed";
             }
        }
    }
}
SilverRay
  • 331
  • 2
  • 7
  • 23
  • Can you post the .php file's code aswell considering it's that file that's doing the uploading and the problem could be with that – Memor-X Dec 10 '15 at 00:18
  • Can somebody help me i tried to edit the code and stretch it but still not working... – SilverRay Dec 10 '15 at 06:25
  • I tried this code from http://stackoverflow.com/questions/25960729/change-form-action-on-select-option but it doesn't still works. Please help... – SilverRay Dec 10 '15 at 07:48
  • I tried also this code http://stackoverflow.com/questions/22843802/change-form-action-based-on-drop-down-selection but it doesn't call the two php scripts to process.... – SilverRay Dec 10 '15 at 08:01
  • I already got my answers here http://stackoverflow.com/questions/34284680/ajax-upload-not-executing-the-php-script?noredirect=1#comment56320533_34284680. Thanks to all who helped me. – SilverRay Dec 22 '15 at 07:36

1 Answers1

0

In case you are using IE8, please know that the above method uses FileReader API of html5 and is not supported in IE8:

http://caniuse.com/#feat=filereader

thepiyush13
  • 1,321
  • 1
  • 8
  • 9
  • no I am using chrome because IE somehow does not support all my codes. – SilverRay Dec 10 '15 at 00:57
  • I found a workaround with this code check this link http://stackoverflow.com/questions/34214951/how-to-upload-text-file-with-dfferent-php-script-process/34260287#34260287 – SilverRay Dec 14 '15 at 05:49