POST FormData到PHP使用JavaScript和XMLHTTPRequest [英] POST FormData to php using javascript and XMLHTTPRequest

查看:158
本文介绍了POST FormData到PHP使用JavaScript和XMLHTTPRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < html> 
< head>
< script>
函数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 =buttononclick =postData()> POST< / button>

< form id = checkBoxes>
< table>
< tr>< input type =checkboxname =opt1value =bluechecked>蓝色与LT; / TD>
< tr>< input type =checkboxname =opt2value =yellow>黄色< / TD>
< / table>
< / form>

< p id =result>< / p>

< / body>
< / html>

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

 <?php 

$ opt1 = $ _ POST ['opt1'];
echo $ opt1;

回声bla;
?>

现在,

 < p id =result>< / p> 

bla显示,但不显示蓝色或黄色。



我做错了什么?

正确的HTML代码如下!

 <!DOCTYPE html> 
< html>
< head>
< meta charset =UTF-8>
< title> POST PHP XMLHTTPRequest< / title>
< script>
函数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 =buttononclick =postData()> POST< / button>

< form id =checkBoxes>
< input type =checkboxname =opt1value =blue>蓝色
< input type =checkboxname =opt2value =yellowchecked>黄色

< / form>

< p id =result>< / p>

< / body>
< / html>


解决方案

blue
$ p $ xmlhttp.setRequestHeader(Content-type,application /的X WWW窗体-urlencoded);

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



删除明确设置内容类型的代码并让浏览器为您生成。 (不要试图将它明确地设置为 multipart / form-data ,你必须指定边界标记将会在标题中)



黄色不会出于同样的原因,但也是因为:




  • 您只查看 opt1 ,它与名称 opt2
  • 复选框控件只有成功(即将在提交的数据中),如果它们被选中(黄色的不是默认的)。


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


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>

and this is what I have in accessdata.php:

<?php

$opt1=$_POST['opt1'];
echo $opt1;

echo "bla";
?>

Now, on

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

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

What am I doing wrong?

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 doesn't show up because you are claiming:

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

But FormData objects encode data as 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 doesn't show up for the same reason, but also because:

  • You are only looking at opt1 and it is associated with the name opt2 and
  • Checkbox controls are only successful (i.e. will be in the data that gets submitted) if they are checked (which the yellow one is not by default).

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).

这篇关于POST FormData到PHP使用JavaScript和XMLHTTPRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆