2

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!

4
  • 1
    you are saying if filename is not empty.. I do not see a "filename" field in the javascript form. There for it is evaluating to false I think. Commented Feb 6, 2015 at 4:54
  • The "filename" field is in HTML on the page, sorry I forgot to mention that. Commented Feb 6, 2015 at 5:40
  • Can you post your entire html? Commented Feb 6, 2015 at 6:24
  • I think if you inspected the element the problem was your html filename field was not actually encapsulated with in the form. Commented Feb 6, 2015 at 6:54

3 Answers 3

1

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
    }
}
?>
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

1

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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.