<bdo id='oixZK'></bdo><ul id='oixZK'></ul>

  1. <i id='oixZK'><tr id='oixZK'><dt id='oixZK'><q id='oixZK'><span id='oixZK'><b id='oixZK'><form id='oixZK'><ins id='oixZK'></ins><ul id='oixZK'></ul><sub id='oixZK'></sub></form><legend id='oixZK'></legend><bdo id='oixZK'><pre id='oixZK'><center id='oixZK'></center></pre></bdo></b><th id='oixZK'></th></span></q></dt></tr></i><div id='oixZK'><tfoot id='oixZK'></tfoot><dl id='oixZK'><fieldset id='oixZK'></fieldset></dl></div>
  2. <legend id='oixZK'><style id='oixZK'><dir id='oixZK'><q id='oixZK'></q></dir></style></legend>
  3. <small id='oixZK'></small><noframes id='oixZK'>

    1. <tfoot id='oixZK'></tfoot>

      使用 javascript 和 XMLHTTPRequest 将 FormData 发布到 php

      POST FormData to php using javascript and XMLHTTPRequest(使用 javascript 和 XMLHTTPRequest 将 FormData 发布到 php)
      <tfoot id='vL09c'></tfoot>

        <tbody id='vL09c'></tbody>
          • <legend id='vL09c'><style id='vL09c'><dir id='vL09c'><q id='vL09c'></q></dir></style></legend>

            <small id='vL09c'></small><noframes id='vL09c'>

            • <bdo id='vL09c'></bdo><ul id='vL09c'></ul>

                <i id='vL09c'><tr id='vL09c'><dt id='vL09c'><q id='vL09c'><span id='vL09c'><b id='vL09c'><form id='vL09c'><ins id='vL09c'></ins><ul id='vL09c'></ul><sub id='vL09c'></sub></form><legend id='vL09c'></legend><bdo id='vL09c'><pre id='vL09c'><center id='vL09c'></center></pre></bdo></b><th id='vL09c'></th></span></q></dt></tr></i><div id='vL09c'><tfoot id='vL09c'></tfoot><dl id='vL09c'><fieldset id='vL09c'></fieldset></dl></div>

                本文介绍了使用 javascript 和 XMLHTTPRequest 将 FormData 发布到 php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                目前我有两个文件,index.htm 和 accessdata.php.这就是我在 index.htm 中的内容:

                At the moment I have two files, index.htm and accessdata.php. This is what I have in index.htm:

                <html>
                <head>
                <script>
                function postData() {
                  var xmlhttp=new XMLHttpRequest();
                  var url = "accessdata.php";
                  var checkBoxes_formData = new FormData(document.getElementById("checkBoxes"));
                
                  xmlhttp.open("POST",url,true);
                  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                  xmlhttp.send(checkBoxes_formData);
                
                  xmlhttp.onreadystatechange=function(){
                    if (xmlhttp.readyState==4 && xmlhttp.status==200){
                      document.getElementById("result").innerHTML=xmlhttp.responseText;
                    }
                  }
                }
                </script>
                
                </head>
                
                <body>
                <button type="button" onclick="postData()">POST</button>
                
                <form id=checkBoxes>
                <table>
                    <tr><input type="checkbox" name="opt1" value="blue" checked> Blue</td>
                    <tr><input type="checkbox" name="opt2" value="yellow"> Yellow</td> 
                </table>
                </form>
                
                <p id="result"></p>
                
                </body>
                </html>
                

                这就是我在 accessdata.php 中的内容:

                and this is what I have in accessdata.php:

                <?php
                
                $opt1=$_POST['opt1'];
                echo $opt1;
                
                echo "bla";
                ?>
                

                现在开始

                <p id="result"></p>
                

                bla"出现,但没有出现blue"或yellow".

                "bla" shows up, but not "blue", or "yellow".

                我做错了什么?

                下面是正确的 HTML 代码!!

                THE CORRECT HTML CODE BELOW!!

                <!DOCTYPE html>
                <html>
                <head>
                <meta charset="UTF-8">
                <title>POST PHP XMLHTTPRequest</title>
                <script>
                function postData() {
                  var xmlhttp=new XMLHttpRequest();
                  var url = "accessdata.php";
                  var checkBoxes_formData = new FormData(document.getElementById("checkBoxes"));
                
                  xmlhttp.open("POST",url,true);
                  xmlhttp.send(checkBoxes_formData);
                
                  xmlhttp.onreadystatechange=function(){
                    if (xmlhttp.readyState==4 && xmlhttp.status==200){
                      document.getElementById("result").innerHTML=xmlhttp.responseText;
                    }
                  }
                }
                </script>
                
                </head>
                
                <body>
                <button type="button" onclick="postData()">POST</button>
                
                <form id="checkBoxes">
                <input type="checkbox" name="opt1" value="blue"> Blue
                <input type="checkbox" name="opt2" value="yellow" checked> Yellow
                
                </form>
                
                <p id="result"></p>
                
                </body>
                </html>
                

                推荐答案

                blue 没有显示,因为您在声明:

                blue doesn't show up because you are claiming:

                xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                

                FormData 对象将数据编码为 multipart/form-data.

                But FormData objects encode data as multipart/form-data.

                删除显式设置内容类型的代码,让浏览器为您生成它.(不要尝试将其显式设置为 multipart/form-data,您还必须在标题中指定边界标记的内容).

                Remove the code that explicitly sets the content-type and let the browser generate it for you. (Don't try to explicitly set it to multipart/form-data, you have to specify what the boundary marker is going to be in the header too).

                yellow 没有出现同样的原因,也是因为:

                yellow doesn't show up for the same reason, but also because:

                • 您只查看 opt1,它与名称 opt2 相关联
                • 复选框控件只有在被选中时才会成功(即将在提交的数据中)(默认情况下不选中黄色).

                更复杂的是,您的 HTML 无效.使用验证器.您不能将输入作为表格行的子项,您需要在它们之间创建一个表格数据单元格.(请注意,您似乎正在尝试使用表格进行布局,您可能应该完全摆脱表格).

                Complicating matters further, your HTML is invalid. Use a validator. You can't have an input as a child of a table row, you need to create a table data cell between them. (Note that it looks like you are trying to use a table for layout, you should probably get rid of the table entirely).

                这篇关于使用 javascript 和 XMLHTTPRequest 将 FormData 发布到 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                When should use doctrine ORM and when zend-db-table?(什么时候应该使用学说 ORM,什么时候应该使用 zend-db-table?)
                Doctrine - self-referencing entity - disable fetching of children(Doctrine - 自引用实体 - 禁用获取子项)
                Doctrine 2, query inside entities(原则 2,实体内部查询)
                Complex WHERE clauses using the PHP Doctrine ORM(使用 PHP Doctrine ORM 的复杂 WHERE 子句)
                Doctrine - OneToMany relation, all result row doesn#39;t fetch in object(Doctrine - OneToMany 关系,所有结果行不获取对象)
                Doctrine and unrefreshed relationships(教义和未更新的关系)
              1. <i id='Usy4n'><tr id='Usy4n'><dt id='Usy4n'><q id='Usy4n'><span id='Usy4n'><b id='Usy4n'><form id='Usy4n'><ins id='Usy4n'></ins><ul id='Usy4n'></ul><sub id='Usy4n'></sub></form><legend id='Usy4n'></legend><bdo id='Usy4n'><pre id='Usy4n'><center id='Usy4n'></center></pre></bdo></b><th id='Usy4n'></th></span></q></dt></tr></i><div id='Usy4n'><tfoot id='Usy4n'></tfoot><dl id='Usy4n'><fieldset id='Usy4n'></fieldset></dl></div>

                  <tbody id='Usy4n'></tbody>

                  <tfoot id='Usy4n'></tfoot>
                    <bdo id='Usy4n'></bdo><ul id='Usy4n'></ul>

                    • <legend id='Usy4n'><style id='Usy4n'><dir id='Usy4n'><q id='Usy4n'></q></dir></style></legend>
                        • <small id='Usy4n'></small><noframes id='Usy4n'>