1

I am doing a small web application. The purpose of this application is storing data to my database. Here is my application process:

First ,the user will upload they file to my web. Here is how file format looks like.

The test.txt will record that each companys sell different items on the date.

test.txt

companyA
date price name
02/12 9.99 name1
...   ... ...
12/01 996.00 name100
companyB
date price name
02/12 9.99 name2
...   ...
12/01 956.00 name200
companyC
date price name
02/12 4.99 name5
...   ...
12/01 996.00 name200
companyD
date price name
02/12 59.99 name1
...   ...
12/01 96.00 name400

Second step: Once use uploaded test.txt. I will display companyA,companyB,companyC and companyD to front-end.(I use <div id="company_result0" style="color:#0A0A0A"></div>).When I display company ,i use radio button for each company name.

    $('#company_result0').append($('<input type="radio" name="companyName" value="' + (key+1) + '" > ' + value + '</input><br />'));

Third step: Let use choose which company information they want to store.

index.html

<form name="uploadform" id="uploadform"  >
<input id="file_browse" type="file" class="btn btn-default text-center defbtntext"  name="fileToUpload">
 </form>
<form class="data-display" id="data-display" name ="data-display">
<div id="company_result0"  style="color:#0A0A0A"></div>
</form>

ajax.js

function getUploadFile() {
        $('#uploadform').on('change', function(e) {
              e.preventDefault();
              var formData = new FormData($(this)[0]);
              $.ajax({
                  type : "POST",
                  url : "findName.php",
                  data : formData,
                  processData: false,
                  contentType: false,
                  success: function (result) {

            var entry = JSON.parse(result);
            $.each(entry ,function(key,value) {
            $('#company_result0').append((key+1));
            $('#company_result0').append(": ");
            $('#company_result0').append($('<input type="radio" name="companyName" value="' + (key+1) + '" > ' + value + '</input><br />'));


        });
 ('#company_result0').append($('<input type="submit" id="submitCompanyName" name="submitCompanyName"</input><br />'));
                 }

              });
        });
}

findName.php

$tempfile = $_FILES ['fileToUpload'] ['tmp_name'];
$fileInfo = new getFileInfo ( $tempfile );
$fileInfo->findcompanyname ();
$fileInfo->sendcompanynametofront ();//send each company name to front.

/**company names will display in #company_result0.
*How can I to get value from #company_result0?
*Here is my code for get value, but it is not work.
*it will show me undefined variable :companyName error.
*/

if ($_SERVER ["REQUEST_METHOD"] == "POST") {
    if (isset($_POST['companyName'])) {
        $companyName=$_POST['companyName'];

    }
}

getFileInfo.php

function sendcompanynametofront() {
    echo json_encode ( companyNameArray );
}

My question is How can I to get value from #company_result0.

When I click submit button in #company_result0 ,it will give the error which is undefined variable :companyName.

Please let me know if there are any misunderstanding.

jpp
  • 49
  • 7
  • jQuery is only aware of the elements in the page at the time it runs, so new elements added to the DOM are unrecognized by jQuery. To combat the problem use [event delegation](http://learn.jquery.com/events/event-delegation/), bubbling events from newly added items up to a point in the DOM which was there when jQuery ran on page load. Many people use `document` as the place to catch the bubbled event, but it isn't necessary to go all the way up the DOM tree. Ideally [you should delegate to the nearest parent existing at the time of page load.](http://stackoverflow.com/a/12824698/1011527) – Jay Blanchard Feb 22 '17 at 19:05
  • do you append separate submit button for each company ?? – hassan Feb 22 '17 at 19:11
  • @HassanAhmed I edited my code. There is only one submit button. – jpp Feb 22 '17 at 19:16

0 Answers0