AJAX PHP文件上传和解析示例

I am looking to take a simple AJAX form that parses a list of emails (via paste in textarea or csv file upload) and imports them into MYSQL after validating them. Here's the HTML for my form:

<form id="form_parser" class="stylized">
                <div style="margin-bottom: 12px;">
                    <h4>Select Software ID:</h4>
                    <select id="selectSoftID" name="selectSoftUnlock">
        <option value="190">Putty</option><option value="356">FileZilla</option>                        </select>
                </div>

                <div style="margin-bottom: 12px;">
                    <h4>Upload Text/CSV File:</h4>
                    <input name="fileEmails" type="file" id="inputFileEmails">
                    <input type="submit" class="button submit" value="">
                </div>

                <div>
                    <h4>Paste in Emails:</h4>
                    <div style="font-size: 12px; margin: -5px 0 6px;">(comma- or line-separated)</div>
                </div>
                <div class="textarea" style="margin-bottom: 4px;"><textarea name="txtEmails"></textarea></div>
                <input type="submit" class="button submit" value="" id="btnSubmitTxtEmails">

            </form>

I was wondering if anyone knew of an example online or could provide some help on this type of thing. I would love for the AJAX response to alert the user how many emails were imported. Any help or links to examples would be really helpful. Thank you!

I think this can be accomplished without AJAX call.

When submit button is clicked, call the JavaScript function available in following link with argument as the value of inputFileEmails. This function is to validate email addresses.

Now, add following event in submit button.

onClick="checkEmail(document.getElementById('inputFileEmails').value);">

checkEmail function is available here:

http://code.activestate.com/recipes/578823-email-address-list-validation/

You may modify this function as per your needs.

If you require AJAX Call:

Following link shows how to send the uploaded file to your target url using AJAX call.

http://portfolio.planetjon.ca/2014/01/26/submit-file-input-via-ajax-jquery-easy-way/

In the above link,

$.ajax( {

  url: 'urlToValidateEmails.php',
  type: 'POST',
  data: new FormData( this ),
  processData: false,
  contentType: false
} );

You may execute this ajax inside some function and call that function using onSubmit event of button in form. Or you can also use jQuery to execute the ajax call (which is also available in link above).

Here, We are using FormData Object to send the data for processing. You can learn more about FormData from the following link:

https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects

Hope this helps!