【发布时间】:2017-04-02 20:19:55
【问题描述】:
我正在尝试发布表单数据,一个数组到 php 文件而不刷新页面。我正在使用似乎不起作用的ajax。下面是表单语法和ajax。有人能帮帮我吗?谢谢。
<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>
在 php 中,我将 function_Options1[] 存储在另一个变量中并进一步使用。
【问题讨论】:
-
使用浏览器调试器查看是否正在发送 ajax 并收到响应。您可能会发现阅读本文很有帮助。 meta.stackoverflow.com/questions/271055
-
您是否使用
</form>关闭了表单?查看您的开发者控制台。这里也没有php。 -
onclick="posttophp"什么都不做;您需要为其添加括号才能调用posttophp()“函数”。 -
@SloanThrasher Tips for asking a good Structured Query Language (SQL) question 与这个问题有什么关系?没有迹象表明他们使用数据库。
-
是的,我已经关闭了表单。添加括号是一个很好的收获。我也添加了这些,但仍然无法正常工作。 PHP 文件很简单,如下所示。
标签: javascript php ajax forms