Horje
On submit upload files to server - Wordpress Solution
I'm trying to submit AJAX form along with multiple files. Though I'm able to store data in DB, but my files are not being uploaded to server. Can anyone help where I'm going wrong? Here is my functions.php code

function savedata() {
	$randomID = uniqid();
	$order_id = rand(1000, 9999);
	global $wpdb;
	$table_name = 'wp_order_quotes_real';
	$countfiles = count($_FILES['file']['name']);
	for ($i = 0; $i < $countfiles; $i++) {
		$filename = $_FILES['file']['name'][$i];
		$upload_dir = wp_upload_dir();
		if (!empty($upload_dir['basedir'])) {
			$user_dirname = $upload_dir['basedir'].
			'/order-quotes/'.$randomID;
			if (!file_exists($user_dirname)) {
				wp_mkdir_p($user_dirname);
			}
		}
		move_uploaded_file($_FILES['file']['tmp_name'][$i], $user_dirname.
			'/'.$filename);
	}
	$dataSubmission = $wpdb - > insert(
		$table_name, array(
			'order_id' => $order_id
			, 'user_id' => $randomID
		)
		, array(
			'%d', '%d
		)
	);
	
	return true;
	exit();
}

add_action('wp_ajax_savedata', 'savedata');
add_action('wp_ajax_nopriv_savedata', 'savedata');
<b>frontend code</b>
$("body").on('click', '#formSubmit', function(e) {
        e.preventDefault();
    	const data = previewAllFields(true);
    	$.ajax({
    		type: "POST",
    		url: ajaxURL,
    		data: {
    			action: "savedata",
    			...data
    		},
    		success: function(data) {
    			alert('Your order has been saved into our database!');
				resetFields();
    		},
    		error: function (error) {
    		    console.log(error, '===error===');
    		}
    	});
    });


Solution - 1

These articles will help you. https://artisansweb.net/ajax-file-upload-in-wordpress/ https://www.phpkida.com/upload-file-using-ajax-in-wordpress/ https://www.ibenic.com/wordpress-file-upload-with-ajax/


Solution - 2

Check if your form have the parameter enctype="multipart/form-data" try to debug your code using print_r($_FILES['file']) and view the structure of the $_FILES array





Wordpress

Related
wordpress ajax load more - Wordpress Solution wordpress ajax load more - Wordpress Solution
[ TABLECONTENT NOT RESPONSIVE ON SMALL SCREENS ] - Wordpress Solution [ TABLECONTENT NOT RESPONSIVE ON SMALL SCREENS ] - Wordpress Solution
Populate entries from repeater field in another field (same form) to add more details. - Wordpress Solution Populate entries from repeater field in another field (same form) to add more details. - Wordpress Solution
Moving Delivery Date Field in Checkout Page - Wordpress Solution Moving Delivery Date Field in Checkout Page - Wordpress Solution
WP Query pagination outputs, but page links do not change the query. - Wordpress Solution WP Query pagination outputs, but page links do not change the query. - Wordpress Solution

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
36