【问题标题】:AJAX PHP Database NOT WORKING no errorsAJAX PHP 数据库不工作没有错误
【发布时间】:2015-12-11 02:32:30
【问题描述】:

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

AJAX

    function submit(){
        var vid = <?php echo $user['vid'] ?>;
        var type = document.getElementById("type").value;
        var rules = document.getElementById("rules").value;
        var comments = document.getElementById("comments").value;
        var xhttp;
        if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.onreadystatechange = function() {
            if (xhttp.readyState == 4 && xhttp.status == 200) {
                document.getElementById("page").innerHTML = xhttp.responseText;
            }
        }
        xhttp.open("POST", "/content/training/request/submit.php", false);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send("vid="+vid+"&type="+type+"&rules="+rules+"&comments="+comments);
    }
</script>

正在调用 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!";

【问题讨论】:

  • 您应该在发送的参数上使用encodeURIComponent(),以防它们包含任何特殊字符。
  • @Barmar 你知道为什么这不起作用吗?并感谢您的意见
  • 检查您的 Apache 错误日志以查看脚本是否失败。

标签: php ajax database


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2011-10-12
    • 2018-05-16
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多