1
Good people, I’m developing a southbox and I’m here with a problem. I write something on Shoutbox and enter or click on enviar
he refresh on the page..
I leave here what I already have ..
INDEX.PHP
/* PARTE DE CIMA DESTA PAGINA */
<?
require('../php/config/config.php');
require('../php/config/classes/framework.php');
$STH = $db->prepare("SELECT * FROM chat");
$STH->execute();
?>
/* PARTE DE CIMA DESTA PAGINA */
<div id="shouts" class="shouts">
<div class="panel-body">
<ul class="list-group">
<?php while ($row = $STH->fetch(PDO::FETCH_ASSOC)) { ?>
<li class="list-group-item"><span><?php echo $row['time'] ?> - </span><strong><?php echo $row['user'] ?>:</strong> <?php echo $row['message'] ?></li>
<?php } ?>
</ul>
</div>
</div>
<div class="panel-footer">
<?php if (isset($_GET['error'])) { ?>
<div class="alert alert-danger">
<?php echo $_GET['error'] ; ?>
</div>
<?php } ?>
<form action="process.php" method="post">
<div class="row">
<div class="col-xs-12 col-sm-6">
<input type="text" class="form-control" name="user" placeholder="Enter Your Name">
</div>
<div class="col-xs-12 col-sm-6">
<input type="text" class="form-control" name="message" placeholder="Enter A Message">
</div>
</div>
<div class="row">
<div class="col-xs-12">
<input class="btn btn-primary shout-btn" type="submit" id="submit" name="submit" value="Shout It Out">
</div>
</div>
</form>
</div>
PROCESS.PHP
<?php
require('../php/config/config.php');
require('../php/config/classes/framework.php');
//check if form submitted
if (isset($_POST['submit'])) {
$user = $_POST['user'];
$message = $_POST['message'];
$time = date('H:i:s');
if (!isset($user) || $user == '' || !isset($message) || $message == '') {
$error = "Please fill in your name and a message";
header('Location: index.php?error='.urlencode($error));
exit();
} else {
$STH = $db->prepare("INSERT INTO chat (user, message, time) VALUES (:user, :message, :time)");
$STH->bindValue(':user', $user);
$STH->bindValue(':message', $message);
$STH->bindValue(':time', $time);
$STH->execute();
header('location: index.php');
exit();
}
}
?>
I put this in the index php.:
<script>
$(document).ready(function(){
$('#submit').click(function(){
$.post("process.php").serialize(), function(response) {
$('#shouts').reload;
});
return false;
});
});
</script>
But to no avail..
My goal was to click
ENTER
or on the button ofSUBMIT
send the message automatically without refreshing the page..
He’ll refresh the page anyway.. And what I wanted Reload to do was wrong for a message to be sent to refresh both the receiving and sending computers.. Facebook-like...
– thecreator
@thecreator ok, one was missing
.closest('form')
But refresh wasn’t supposed to happen. Take a look at this example and use the code as it is -> http://jsfiddle.net/9ogrdhvs/ If you have problems check the console and tell me if there are any errors there...– Sergio
Now it does not refresh as wanted, but does not send anything to the database..
– thecreator
You can see in the Developer tools what is passed in the request? What gives var_dump($_POST); ?
– Sergio
I didn’t realize it from Veloper tools.. I’m doing it in shamp.. and in about 20 minutes I’ll come home and var_dump it. Thanks so much for your help and patience !
– thecreator
array(0) { }
gave nothing..– thecreator
I took
e.preventDefault();
and started sending to the database but gives refresh on the page without it .. I do not understand..:(– thecreator
I was suggesting you use https://developer.chrome.com/devtools, by pressing CTR + SHIFT + I, or F12. It does
console.log(this.tagName, data);
after the linevar data = $(this).serialize();
. What gives?– Sergio
Whenever I try to send something give me this error: http://i.imgur.com/sOMQK5z.png but nothing ever happens on the page..
– thecreator