0

所以我正在用 PHP/PDO/AJAX 制作一个基本的聊天框,最近当我添加 AJAX 代码并提交我的喊叫时,它运行了两次查询,我检查了我的代码,检查员说他们看不到哪里不对了。所以我想知道你是否可以帮我找到错误。

喊叫.php

<html>
<head>
    <title>Shout!</title>
    <link  rel="stylesheet" href="CSS/styles.css"/>
</head>  
</html>

<?php

include 'auth.login.php';
include 'pdo.config.php';

if (!isset($_SESSION['Username'])) {
echo '<br/>';
echo '<center>You need to login to post!<br/>';
header("Refresh:2; URL=index.php");
exit();
}

$Username = $_SESSION['Username'];

if (!isset($_POST['Message']) || empty($_POST['Message'])) {
echo '<br/>';
echo '<center>How do you expect to send a message if the field is empty, ey?<br/>';
header("Refresh:2; URL=index.php");
exit();
} else {

$Message = htmlspecialchars($_POST['Message']);

$insertMessage = $PDO->prepare("INSERT INTO `chatbox` (User, Message) VALUES (?, ?)");
$insertMessage->bindParam(1, $Username, PDO::PARAM_STR);
$insertMessage->bindParam(2, $Message, PDO::PARAM_STR);

if ($insertMessage->execute()) {
    header("Location: index.php");
    exit();
} else {
    echo '<br/>';
    echo '<center>Aw snap, shout could not be added!';
    header("Refresh:2; URL=index.php");
    exit();
}
}

?>

main.js

$(document).ready(function(){

$.ajax({

    url: 'data.php',
    data: '',
    dataType: 'json',
    type: 'GET',
    success: function(data)
    { 
$.each(data, function (idx, elem) {
$('#messageDisplay').append("<div>["+elem.SentOn+"] "+elem.User+": "
+ elem.Message + "</div>");
        });
    }
});
});

我的表格:

<form action="shout.php" method="post">
<textarea rows="8" cols="74" id="Message" name="Message" placeholder="Post messages here."></textarea> 
<br/><input type="submit" id="submit" value="Shout!">
                </form>

数据.php

<?php

include 'pdo.config.php';


$chat = $PDO->query("SELECT * FROM `chatbox`");
$getRow = $chat->fetchAll(PDO::FETCH_ASSOC);

echo json_encode($getRow);

?>
4

1 回答 1

0

下一步是检查您的 access.log 以确保您没有从某处获得双重 POST 到 say.php

可能还有其他帖子被忽视了。

于 2013-09-04T23:00:01.233 回答