PHP无法解析Javascript创建的HTML表单

I have HTML forms being created by Javascript. I also have regular forms not created by Javascript. When I submit the regular HTML form, the PHP script parses it just fine, but when I submit the Javascript created forms, it's not parsed. I can't for the life of me figure out why one parses, and the other one doesn't.

Javascript:

function addWeapon(){
    var weapsForm = document.createElement("form");
    weapsForm.setAttribute('name',"savefile");
    weapsForm.setAttribute('method',"post");
    weapsForm.setAttribute('action',"");

    var weapsName = document.createElement("input");
    weapsName.setAttribute('type',"text");
    weapsName.setAttribute('name',"textdata[]");

    var weapsNameQt = document.createElement("input");
    weapsNameQt.setAttribute('type',"number");
    weapsNameQt.setAttribute('name',"textdata[]");
    weapsNameQt.setAttribute('value',"0");

    var weapsNameSubmit = document.createElement("input");
    weapsNameSubmit.setAttribute('type',"submit");
    weapsNameSubmit.setAttribute('name',"submitsave");
    weapsNameSubmit.setAttribute('value',"Done!");

    weapsForm.appendChild(weapsName);
    weapsForm.appendChild(weapsNameQt);
    weapsForm.appendChild(weapsNameSubmit);

    document.getElementsByTagName('body')[0].appendChild(weapsForm);
}

And here's the PHP code which parses the HTML form no problem. I'd also like to add, the Javascript created HTML is exactly the same as the regular HTML.

PHP:

<?php
if (isset($_POST)){
    if ($_POST['submitsave'] == "Done!"  && !empty($_POST['filename'])) {
        foreach($_POST["textdata"] as $text){
            if(!file_exists($_POST['filename'] . ".txt")){
                $file = tmpfile();
            }
            $file = fopen($_POST['filename'] . ".txt","a+");
            while(!feof($file)){
                $old = $old . fgets($file);
            }
        file_put_contents($_POST['filename'] . ".txt", trim($text).PHP_EOL, FILE_APPEND);
        }
    fclose($file);
    }
}
?>

Thanks for any help guys!

EDIT: I forgot to mention the "filename" field is in the HTML document, and is not created by Javascript.

EDIT: Also, the file is created on the server, but the forms are not being parsed.

EDIT: After messing with the code for a couple of hours, I realized it was in fact not parsing the "filename" field on the HTML document, but was parsing the Javascript form. Thank you guys for the help!

The form you are making with the Javascript is missing the filename field.. You either need to add the filename field in the Javascript for like below:

function addWeapon(){
    var weapsForm = document.createElement("form");
    weapsForm.setAttribute('name',"savefile");
    weapsForm.setAttribute('method',"post");
    weapsForm.setAttribute('action',"");

    var weapsName = document.createElement("input");
    weapsName.setAttribute('type',"text");
    weapsName.setAttribute('name',"textdata[]");

    var weapsNameQt = document.createElement("input");
    weapsNameQt.setAttribute('type',"number");
    weapsNameQt.setAttribute('name',"textdata[]");
    weapsNameQt.setAttribute('value',"0");

    var weapsfilename = document.createElement("input");
    weapsfilename.setAttribute('type',"text");
    weapsfilename.setAttribute('name',"filename");
    weapsfilename.setAttribute('value',"SomeFile.txt");

    var weapsNameSubmit = document.createElement("input");
    weapsNameSubmit.setAttribute('type',"submit");
    weapsNameSubmit.setAttribute('name',"submitsave");
    weapsNameSubmit.setAttribute('value',"Done!");

    weapsForm.appendChild(weapsName);
    weapsForm.appendChild(weapsNameQt);
    weapsForm.appendChild(weapsNameSubmit);

    document.getElementsByTagName('body')[0].appendChild(weapsForm);
}

Or you need to change your conditional in php not to be based on the filename like so:

<?php
if (isset($_POST)){
    if ($_POST['submitsave'] == "Done!") {
        // New code parsing the other fields since you are no longer grabbing a file
    }
}
?>

I gues your $_POST['filename'] is empty. empty() function in php check is variable has some value. So if you do not choose your file it will return true ( empty ). Try to use isset() if you need to check is your input was sent.

Looking at your code it seems that you need to add following code in your javascript.

function addWeapon(){
var weapsForm = document.createElement("form");
weapsForm.setAttribute('name',"savefile");
weapsForm.setAttribute('method',"post");
weapsForm.setAttribute('action',"");

var weapsName = document.createElement("input");
weapsName.setAttribute('type',"text");
weapsName.setAttribute('name',"textdata[]");

var weapsNameQt = document.createElement("input");
weapsNameQt.setAttribute('type',"number");
weapsNameQt.setAttribute('name',"textdata[]");
weapsNameQt.setAttribute('value',"0");

var weapsFile = document.createElement("input");
weapsFile.setAttribute('type',"file");
weapsFile.setAttribute('name',"filename");
weapsFile.setAttribute('value',"0");

var weapsNameSubmit = document.createElement("input");
weapsNameSubmit.setAttribute('type',"submit");
weapsNameSubmit.setAttribute('name',"submitsave");
weapsNameSubmit.setAttribute('value',"Done!");

weapsForm.appendChild(weapsName);
weapsForm.appendChild(weapsNameQt);
weapsForm.appendChild(weapsFile);
weapsForm.appendChild(weapsNameSubmit);

document.getElementsByTagName('body')[0].appendChild(weapsForm);
}

And in php you need to print $_FILES instead of $_POST to get filename.