Bom dia galera. Estou com uma dúvida e gostaria de saber se tem solução... vamos lá: Tenho uma pagina PHP com um form para cadastro, ao clicar em "Adicionar" gostaria que uma DIV, na verdade uma tablea dentro da div fosse atualizada sem refresh. Os códigos estão abaixo: Tela de cadastro: Código fonte index.php <html>
<head>
<title></title>
<script language="javascript" src="ajax.js"></script>
<script>
window.onload = statusAlterado;
</script>
<style>
<!--
body {
background: #f7f7ff;
}
#geral {
font: bold 12px trebuchet MS :
color : blue;
position: relative;
top: 35%;
left: 35%;
}
p {
text-align: center;
font: bold 15px verdana, arial;
color: red;
position: absolute;
top: 200px;
left: 200px;
}
legend {
font: bold 12px trebuchet MS;
color: #193935;
text-transform: uppercase;
}
-->
</style>
</head>
<body>
<div id="geral">
<fieldset style="width: 300px"><legend> Cadastro </legend>
<form id="frm">
<center>Nome: <input type="text" name="nome" id="nome"><br>
Senha: <input type="password" name="senha" id="senha"><br><br>
<input type="button" value="cadastrar" onclick="cadastra();"></center>
</form>
</fieldset>
</div>
<div id="resultados"><b>
<table align="center" border="1" id="tabela">
<tr style="margin-left: 10px" align="center" >
<td >Nome</td>
<td >Excluir</td>
</tr>
<?
include_once "conexao.php";
$sql = "SELECT * FROM clientes";
$rs = mysql_query($sql, $conn);
$linha = 0;
while ($linha = mysql_fetch_array($rs)) {
$linha++;
?>
<tr align="center" >
<td ><?echo $linha['nome_cliente'];?></td>
<td ><a href="#" onClick="apagar('<?echo $linha['id_cliente'];?>', this.parentNode.parentNode.rowIndex);">X</a></td></a></td>
</tr>
<?
}
mysql_close($conn);
?>
</table>
</b></div>
</body>
</html>
Código fonte ajax.js
function getXmlHttp() {
var xmlhttp;
try {
xmlhttp = new XMLHttpRequest();
} catch (ee) {
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
}
return xmlhttp;
}
var request = getXmlHttp();
function cadastra() {
var nm = document.getElementById('nome').value;
var se = document.getElementById('senha').value;
var url = "cadastra.php?nome=" + nm + "&senha=" + se;
request.open('GET', url, true);
request.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
document.forms[0].reset();
request.send(null);
request.onreadystatechange = statusAlterado;
}
function apagar(id, rowIndex) {
if (confirm('Tem certeza que deseja excluir este registro?')) {
document.getElementById("tabela").deleteRow(rowIndex);
request.open("POST", 'apagar.php?Deletar=Ok&id=' + id, false);
request.send(null);
}
}
function statusAlterado() {
if (request.readyState == 4) {
document.getElementById("resultados").innerHTML = request.responseText;
}
}
Código do cadastra.php responsável pela inserção no banco.
<?php
include_once "conexao.php";
$nm = $_GET['nome'];
$se = $_GET['senha'];
$sql = "INSERT INTO clientes(nome_cliente, senha) VALUES ('$nm','$se')";
$rs = mysql_query($sql, $conn);
if(!$rs)
echo "Não foi possivel realizar seu cadastro!";
else
echo $nm;
mysql_close($conn);
?> Bem, o que acontece no momento é que ao acessar a pagina tudo beleza, mas depois que eu insiro no banco a tabela some e aparece o nome que eu acabei de cadastrar... alguém pode me dar uma luz?? Grato, Bokerão