0

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

<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>
            <?php if (isset($file)) { ?>
                <tr>
                    <th align="right" valign="top">Delete File:</th>
                    <td><input type="checkbox" name="chkdel" value="yes"> <i><b>Leave unchecked if uploading a new file. File will be replaced with the new one.</b></i></td>
                </tr>
            <?php } ?>
            <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>
                    <?php if (isset($file)) { ?><input type="hidden" name="oldfile" value="<?php echo $file; ?>" /><?php } ?>
                </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;
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.