Skip to content

Commit 6495b05

Browse files
committed
Add support for file uploads
Currenly only supports one file per input, and needs some testing.
1 parent 55420de commit 6495b05

2 files changed

Lines changed: 51 additions & 8 deletions

File tree

assets/scripts/wplf-form.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,58 @@
77
$(document).ready(function() {
88

99
// ajax form submissions
10-
$('.libre-form').submit(function(e) {
10+
$('.libre-form').submit(function(e) {
1111

1212
var $form = $(this);
1313

14-
// add class to enable css changes to indicate ajax loading
14+
// add class to enable css changes to indicate ajax loading
1515
$form.addClass('sending');
1616

1717
// reset errors
1818
$form.find('.wplf-error').remove();
1919

20+
var data = new FormData($form[0]); // includes files too, but for some reason we don't see them at the server
21+
22+
23+
window.data = data;
24+
25+
26+
fetch(ajax_object.ajax_url + '?action=wplf_submit', {
27+
method: "POST",
28+
body: data
29+
}).then(function(response){
30+
//console.log(response);
31+
return response.text();
32+
}).then(function(response){
33+
console.log(response);
34+
response = JSON.parse(response);
35+
if( 'success' in response ) {
36+
// show success message if one exists
37+
$form.after(response.success);
38+
}
39+
if( 'ok' in response && response.ok ) {
40+
// submit succesful!
41+
$form.remove();
42+
}
43+
if( 'error' in response ) {
44+
// show error message in form
45+
$form.append('<p class="wplf-error error">' + response.error + '</p>');
46+
}
47+
$form.removeClass('sending');
48+
}).catch(function(error){
49+
console.log("Fetch error: ", error);
50+
$form.removeClass('sending');
51+
});
52+
2053
// submit form to ajax handler in admin-ajax.php
21-
$.post( ajax_object.ajax_url + '?action=wplf_submit',
22-
$(this).serialize(),
54+
/*$.post( ajax_object.ajax_url + '?action=wplf_submit',
55+
$(this).serialize(),
2356
function(response) {
2457
console.log(response);
2558
if( 'success' in response ) {
2659
// show success message if one exists
2760
$form.after(response.success);
28-
}
61+
}
2962
if( 'ok' in response && response.ok ) {
3063
// submit succesful!
3164
$form.remove();
@@ -38,7 +71,7 @@ $(document).ready(function() {
3871
).always(function() {
3972
// finished XHR request
4073
$form.removeClass('sending');
41-
});;
74+
});*/
4275

4376
// don't actually submit the form, causing a page reload
4477
e.preventDefault();
@@ -48,4 +81,3 @@ $(document).ready(function() {
4881
});
4982

5083
})(jQuery);
51-

inc/wplf-ajax.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ function wplf_ajax_submit_handler() {
1818

1919
if( $return->ok ) {
2020
// form existence has already been validated via filters
21+
22+
2123
$form = get_post( intval( $_POST['_form_id'] ) );
2224

2325
// the title is the value of whatever the first field was in the form
@@ -52,6 +54,16 @@ function wplf_ajax_submit_handler() {
5254
}
5355
}
5456

57+
// handle files
58+
foreach( $_FILES as $key => $file) {
59+
// Is this enough security wise?
60+
// Currenly only supports 1 file per input
61+
$attach_id = media_handle_upload( $key, 0, array(), array( "test_form" => false ) );
62+
add_post_meta($post_id, $key, $attach_id);
63+
}
64+
65+
66+
5567
$return->submission_id = $post_id;
5668
$return->submission_title = $post_title;
5769

@@ -68,4 +80,3 @@ function wplf_ajax_submit_handler() {
6880
wp_send_json( $return );
6981
wp_die();
7082
}
71-

0 commit comments

Comments
 (0)