POST FormData到PHP使用JavaScript和XMLHTTPRequest [英] POST FormData to php using javascript and XMLHTTPRequest
问题描述
这就是我在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>
但是 删除明确设置内容类型的代码并让浏览器为您生成。 (不要试图将它明确地设置为 更复杂的是,您的HTML无效。 使用验证器。您不能将输入作为表格行的子项,您需要在它们之间创建表格数据单元格。 (请注意,它看起来像是在尝试使用表格进行布局,您应该完全摆脱表格)。 At the moment I have two files, index.htm and accessdata.php.
This is what I have in index.htm: and this is what I have in accessdata.php: Now, on "bla" shows up, but not "blue", or "yellow". What am I doing wrong? THE CORRECT HTML CODE BELOW!!
But 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 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屋! blue $ c
$ p $ xmlhttp.setRequestHeader(Content-type,application /的X WWW窗体-urlencoded);
FormData
对象将数据编码为 multipart / form-data
。
multipart / form-data
,你必须指定边界标记将会在标题中)
黄色
不会出于同样的原因,但也是因为:
opt1
,它与名称 opt2 $ c $关联
<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>
<?php
$opt1=$_POST['opt1'];
echo $opt1;
echo "bla";
?>
<p id="result"></p>
<!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");
FormData
objects encode data as multipart/form-data
.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:
opt1
and it is associated with the name opt2
and