Javascript 在不刷新页面的情况下将表单数据发布到php
我试图发布表单数据,一个数组到php文件,而不刷新页面。我使用的是ajax,它似乎不起作用。下面是表单语法和ajax。谁能帮帮我吗?谢谢Javascript 在不刷新页面的情况下将表单数据发布到php,javascript,php,ajax,forms,Javascript,Php,Ajax,Forms,我试图发布表单数据,一个数组到php文件,而不刷新页面。我使用的是ajax,它似乎不起作用。下面是表单语法和ajax。谁能帮帮我吗?谢谢 <form name="postcontent" id="postcontent"> function[]; //just for an example of data <button type="button" id="submitform" onclick="posttophp" >Send</button> <
<form name="postcontent" id="postcontent">
function[]; //just for an example of data
<button type="button" id="submitform" onclick="posttophp" >Send</button>
<script>
function posttophp() {
var xhttp = new XMLHttpRequest();
xhttp1.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("submitform").innerHTML = this.responseText;
}
};
xhttp.open("POST", "options.php", true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("
function_Options1[]=function[]")
}
</script>
功能[]//仅举一个数据示例
发送
函数posttophp(){
var xhttp=newXMLHttpRequest();
xhttp1.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“submitform”).innerHTML=this.responseText;
}
};
open(“POST”,“options.php”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhttp.send(“
功能选项1[]=功能[]”)
}
在php中,我将函数_Options1[]存储在另一个变量中并进一步使用 有两个想法可以尝试:
onclick=“posttohp();”
使用浏览器调试器查看是否发送ajax以及是否收到响应。你可能会发现读这本书很有帮助。您是否用
关闭了表单?看看你的开发者控制台。这里也没有php。onclick=“posttohp”
它什么都不做;您需要为它添加括号,以便调用posttophp()
“函数”。@sloachrisher与此问题有什么关系?没有迹象表明他们在使用数据库。是的,我已经关闭了表单。添加括号是一个很好的技巧。我也添加了这些,但仍然不起作用。Php文件很简单,如下所示。