dth981485742 2015-02-06 04:50
浏览 38
已采纳

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!

  • 写回答

3条回答 默认 最新

  • dougong8012 2015-02-06 04:58
    关注

    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
        }
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?