AJAX PHP数据库不工作无错误

AJAX PHP数据库不工作无错误,php,ajax,database,Php,Ajax,Database,我是AJAX新手,正在尝试使用AJAX和php将表单数据插入数据库。它根本不起作用,我一开始就有错误,但现在没有更多的错误,数据库也没有更新,也没有响应文本。请帮忙 阿贾克斯 函数提交(){ var-vid=; var type=document.getElementById(“type”).value; var rules=document.getElementById(“规则”).value; var comments=document.getElementById(“comments”).

我是AJAX新手,正在尝试使用AJAX和php将表单数据插入数据库。它根本不起作用,我一开始就有错误,但现在没有更多的错误,数据库也没有更新,也没有响应文本。请帮忙

阿贾克斯

函数提交(){
var-vid=;
var type=document.getElementById(“type”).value;
var rules=document.getElementById(“规则”).value;
var comments=document.getElementById(“comments”).value;
var-xhttp;
if(window.XMLHttpRequest){
xhttp=newXMLHttpRequest();
}否则{
//IE6、IE5的代码
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.onreadystatechange=函数(){
如果(xhttp.readyState==4&&xhttp.status==200){
document.getElementById(“page”).innerHTML=xhttp.responseText;
}
}
open(“POST”,“/content/training/request/submit.php”,false);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xhttp.send(“vid=“+vid+”&type=“+type+”&rules=“+rules+”&comments=“+comments”);
}
PHP文件AJAX正在调用:

<?php
require("http://".$_SERVER["HTTP_HOST"]."/config/db.php");
$stmt = $db->prepare("INSERT INTO trainingRequests (vid, type, rules, comments, timeSubmitted) VALUES (:vid,:type,:rules,:comments,:timeSubmitted)"); 
$stmt->bindParam(':vid', $vid);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':rules', $rules);
$stmt->bindParam(':comments', $comments);
$stmt->bindParam(':timeSubmitted', $time);

$vid = $_POST["vid"];
$type = $_POST["type"];
$rules = $_POST["rules"];
$comments = $_POST["comments"];
$time = time();

$stmt->execute();

echo "Submitted!";

不要在
require
中使用HTTP URL。当您通过Web服务器访问PHP脚本时,它运行脚本(在另一个服务器实例中,因此不会影响当前脚本),而不会返回脚本内容。将其更改为:

require("../../config/db.php");

您应该在发送的参数上使用
encodeURIComponent()
,以防它们包含任何特殊字符。@Barmar您知道为什么这不起作用吗??感谢您的输入检查您的Apache错误日志以查看脚本是否失败。
require("../../config/db.php");