phpformshttp-postform-submitmultiple-forms

Second form in page not working


I am having trouble getting anything to post from the second form in this class.

The first form uploads the file fine, and I am able to parse it into an array. However, the second form that I echo on page does nothing on submit.

Got any ideas?

Thanks!

public function __construct () {
    add_action('admin_menu', array( $this, 'asbcb' ) );
}

function asbcb() {
    $page_title = 'Build';
    $menu_title = 'Build'; 
    $capability = 'manage_options';
    $menu_slug  = 'build';
    $function   = array( $this, 'sbcb' );
    add_management_page($page_title,$menu_title,$capability,$menu_slug,$function);
}

function sbcb() {
    $output = '';
    $output .= '<form id="upload" action="" method="post" enctype="multipart/form-data">';
    $output .= '<p><input type="file" name="file"></p>';
    $output .= get_submit_button("Upload","primary","upload");
    $output .= '</form>';
    echo $output;
    $this->upload();
}

function upload() {
    if(isset($_POST['upload']) && ! empty($_FILES)) {
        $file   = $_FILES['file'];
        $dir = plugin_dir_path( __FILE__);
        $path = $dir.'pages/';
        if(!is_dir($path)) mkdir($path);
        $url = $path.$file['name'];
        $uploaded = move_uploaded_file($file['tmp_name'], $path.$file['name']);
        if ($uploaded) {
            $this->parse_CSV($url);
        } else {
            print_r($file['error']);
        }
    }
}

function parse_csv($csv) {
    $columns = array();
    if (($handle = fopen($csv, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            $columns[] = $data;
        }
        fclose($handle);
    }
    $this->fields();
}

function fields(){
    $output = '';
    $output .= '<form id="build" action="" method="post">';
    $output .= '<select class="fields" name="fields[]">';
    $output .= '<option value="title">Title</option>';
    $output .= '<option value="slug">Slug</option>';
    $output .= '<option value="metatitle">Meta title</option>';
    $output .= '<option value="metadesc">Meta description</option>';
    $output .= '<option value="template">Template</option>';
    $output .= '<option value="metakey">Meta key</option>';
    $output .= '<option value="metavalue">Meta value</option>';
    $output .= '</select> ';
    $output .= get_submit_button("Build","primary","build");
    $output .= '</form>';
    echo $output;
    $this->fields_to_array();
} 

function fields_to_array() {
    if (isset($_POST['fields'])) {
        $fields = $_POST['fields'];
        var_dump($fields);

    }
}

Solution

  • If you want all form data to be sent on the same action, you'll need to merge the fields into a single form. The form tags exclude data from fields that exist outside of the submitted form.


    Otherwise, you might hack something up like getting javascript to intercept the submissions from either form, and manually pull data from both forms and POST it all to the receiving file via javascript. (I don't think I'd recommend this way though.)