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!