Help with file upload with AJAX and php

[ad_1]

I have a form that I am trying to upload a file to the server with and then save the info to the database. Saving the info to the database works fine except for the file information and the file doesn’t upload. My code is below. Any help is appreciated.

**Form**

<table>
<form action=”<?php echo site_url() ?>/wp-admin/admin-ajax.php” method=”POST” id=”editeventform” class=”editeventform” enctype=”multipart/form-data”>
<input type=”hidden” name=”eventid” id=”eventid” value=”<?php echo $id; ?>”>
<table class=”editeventtable”>
<tr>
<th align=”right” valign=”top”>Title:</th>
<td><input type=”text” name=”title” id=”title” placeholder=”Title” value=”<?php echo $title; ?>” /></td>
</tr>
<tr>
<th align=”right” valign=”top”>Date:</th>
<td><input type=”date” name=”newdate” id=”newdate” placeholder=”mm/dd/yyyy” value=”<?php echo $newdate; ?>” /> <i><b>Format as mm/dd/yyyy</b></i></td>
</tr>
<tr>
<th align=”right” valign=”top”>Time:</th>
<td><input type=”time” name=”newtime” id=”newtime” placeholder=”hh:mm am/pm” value=”<?php echo $newtime; ?>” /> <i><b>Format as hh:mm am or pm</b></i></td>
</tr>
<tr>
<th align=”right” valign=”top”>Upload New File:</th>
<td>
<input type=”file” name=”infofile” id=”infofile” accept=”.pdf”> <i><b>This will overwrite any file already uploaded.</b></i>
</td>
</tr>
<tr>
<th align=”right” valign=”top”>Information:</th>
<td>
<?php
//$content = $info;
$editor_id = ‘info’;
$settings = array(
‘textarea_name’ => ‘info’,
‘editor_height’ => 425,
‘media_buttons’ => FALSE,
‘tinymce’ => array(
‘width’ => 1000,
)
);
wp_editor( $info, $editor_id, $settings );
?>
</td>
</tr>
<tr>
<td colspan=”2″ align=”center”>
<button>Save Edit</button>
<input type=”hidden” name=”action” value=”editeventform”>
</td>
</tr>
</table>
</form>

**jQuery**

jQuery(function ($) {
$(‘.editeventform’).submit(function () {
event.preventDefault();
var editeventform = $(‘.editeventform’);
$.ajax({
url: editeventform.attr(‘action’),
data: editeventform.serialize(), // form data
type: editeventform.attr(‘method’), // POST
success: function (data) {
$(‘#response’).html(data); // Display Results
}
});
return false;
});
});

**PHP**

add_action(‘wp_ajax_editeventform’, ‘save_edit_form’);
add_action(‘wp_ajax_nopriv_editeventform’, ‘save_edit_form’);
function save_edit_form() {
global $wpdb;
global $wp_filesystem_direct;

$user_id = get_current_user_id();
$id = $_POST[“eventid”];
$title = $_POST[‘title’];
$date = $_POST[‘newdate’];
$newdate = date(“Y-m-d”, strtotime($date));
$time = $_POST[‘newtime’];
$newtime = date(“G:i”, strtotime($time));
$info = wpautop($_POST[‘info’]);
if (!empty($_FILES[‘infofile’][‘name’])) {
if ( ! function_exists( ‘wp_handle_upload’ ) ) {
require_once( ABSPATH . ‘wp-admin/includes/file.php’ );
}
$filename = $_FILES[‘infofile’][‘name’];
$table = $wpdb->prefix . “events”;
$data = array(
‘title’ => $title,
‘date’ => $newdate,
‘time’ => $newtime,
‘info’ => $info,
‘filename’ => $filename,
);
$where = array(
‘id’ => $id
);
$wpdb->update($table, $data, $where);
$dirfile = wp_upload_dir();
$dirname = $dirfile[‘basedir’] . ‘/communitybb_events/files/’ . $id . ‘/’;
wp_mkdir_p($dirname);
$source = $_FILES[‘infofile’][‘tmp_name’];
$destination = $dirname . $_FILES[‘infofile’][‘name’];
move_uploaded_file($source, $destination);
} else {
$table = $wpdb->prefix . “events”;
$data = array(
‘title’ => $title,
‘date’ => $newdate,
‘time’ => $newtime,
‘info’ => $info,
);
$where = array(
‘id’ => $id
);
$wpdb->update($table, $data, $where);
}
?>
<h3><?php echo $title; ?> Has Been Updated</h3>
<?php
die;
}

&#x200B;

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer