Dados são inseridos no BD, mas não carregam sem refresh
Boa noite pessoal, estou tentando fazer com que dados de um formulário sejam gravados no bd e carregados dentro de uma div sem refresh. Ele está inserindo no banco sem problemas, porém não carrega o registro inserido dentro da div, vou postar abaixo meus arquivos:
index.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 13px;
}
h1 {
font-size: 18px;
color: #069;
border-bottom: 1px #999 dashed;
}
input, textarea {
font-family: Georgia, "Times New Roman", Times, serif;
font-size: 13px;
padding: 5px;
border: 1px #999 solid;
background: #F9F9F9;
}
#status {
padding: 5px 0 15px 0;
display: none;
color: #900;
font-weight: bold;
}
</style>
<script type="text/javascript" language="javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript" language="javascript">
$(function($) {
$("#formulario").submit(function() {
var nome = $("#nome").val();
var email = $("#email").val();
var mensagem = $("#mensagem").val();
$("#status").html("<img src='loader.gif' alt='Enviando' />");
$.post('envia.php', {nome: nome, email: email, mensagem: mensagem }, function(resposta) {
$("#status").slideDown();
if (resposta != false) {
$("#status").html(resposta);
}
else {
$("#status").html("Mensagem enviada com sucesso!");
$("#mensagens").prepend("<strong>"+ nome +"</strong> disse: <em>" + mensagem + "</em><br />");
$("#nome").val("");
$("#email").val("");
$("#mensagem").val("");
}
});
});
});
</script>
</head>
<body>
<h1>Escrever Mensagem</h1>
<div id="status"></div>
<div id="escrever">
<form id="formulario" action="javascript:func()" method="post">
<label for="name">Nome</label>
<input type="text" name="nome" id="nome"/>
<label for="em">Email</label>
<input type="text" name="email" id="email"/>
<label for="em">Comentário</label>
<input type="text" name="mensagem" id="mensagem"/>
<input type="submit" value="Postar Comentário" name="postar" />
</form>
</div>
<h1>Mensagens</h1>
<div id="mensagens">
<?php
include 'SQLInstructions.php';
$sql = new SQLInstructions();
$show = $sql->Open();
foreach ($show as $mensagem) {
echo "<strong>" . $mensagem->nome . "</strong> disse: <em>" . $mensagem->mensagem . "</em><br />";
}
?>
</div>
</body>
</html>
envia.php
<?php
// Incluimos o arquivo de conexão
include 'SQLInstructions.php';
$sql = new SQLInstructions();
extract($_POST);
echo $sql->Insert($nome,$email,$mensagem);
isset($_POST);
?>
SQLInstructions.php
<?php
class SQLInstructions {
private $host = "mysql:host=localhost;port=3306;dbname=banco";
private $user = "root";
private $senha = "";
public function __construct(){}
// inserir
public function Insert($nome,$email,$mensagem){
try{
$con = new PDO($this->host,$this->user,$this->senha);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con -> exec("set names utf8");
$sql = "INSERT INTO mensagens(nome,email,mensagem) VALUES('$nome','$email','$mensagem')";
$st = $con->prepare($sql);
$st->execute();
return 'Post Inserido com sucesso';
}
catch (Exception $e){
return $e->getMessage();
}
}
/***Teste***/
public function Open() {
try{
$con = new PDO($this->host,$this->user,$this->senha);
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con -> exec("set names utf8");
$sql = "SELECT * FROM mensagens";
$st = $con->prepare($sql);
$st->execute();
$vetor = array();
while($linha = $st->FETCH(PDO::FETCH_OBJ)) {
array_push($vetor, $linha);
}
return $vetor;
}
catch (Exception $e){
return $e->getMessage();
}
} //listar
}
?>
Peço que se possível me indiquem onde posso ter errado.Discussão (5)
Carregando comentários...