【发布时间】:2013-12-19 23:42:55
【问题描述】:
我正在使用 AJAX 将输入从网页发送到 PHP 文件,然后再输入到数据库中。这是我的 JavaScript 文件:
var pageLoaded = function () {
var submitButton = document.getElementById("submit");
if (submitButton) {
submitButton.addEventListener("click", submit, true);
}
};
var submit = function () {
var xhr, changeListener;
var form = document.getElementById('item_form');
var inputs = form.getElementsByTagName('input');
// create a request object
xhr = new XMLHttpRequest();
// initialise a request, specifying the HTTP method
// to be used and the URL to be connected to.
xhr.open("POST", "../php/add_item.php", true);
console.log(inputs[0].value); // debugging
// Sends the inputs to the add_item.php file
xhr.send(inputs);
};
window.onload = pageLoaded;
在这里,我尝试将表单中的输入发送到位于我的文件系统中 "../php/add_item.php" 的名为 add_item.php 的 PHP 文件。
我很确定这段代码可以工作,并将输入以数组的形式发送到 PHP 文件。
我的问题是,我如何在该文件中使用 $_REQUEST 才能使用数组中的输入发送到数据库?或者,最好的方法是什么?
【问题讨论】:
标签: javascript php ajax arrays xmlhttprequest