Send message and update div without giving Reload the page

Asked

Viewed 1,319 times

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 of SUBMIT send the message automatically without refreshing the page..

1 answer

4

You got a mistake on this line:

$.post("process.php").serialize(), function (response) {

The arguments of the function are not correct. The correct syntax is:

jQuery.post(url [, data] [, fn success] [, dataType ]);

You’re using return false; to stop the form, I prefer e. preventDefault(); which is semantically more correct.

I imagine your form was submitted because of the error, without it I think the code would work and Submit would be via ajax and without refresh of the page.

My suggested code would be:

$('#submit').click(function (e) {
    e.preventDefault();
    var data = $(this).serialize();
    $.post("process.php", data, function (response) {
        alert('ajax feito!');
        $('#shouts').reload; // repara esta linha não faz nada! seria talvez `.reload()`?
    });
  • 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 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...

  • Now it does not refresh as wanted, but does not send anything to the database..

  • You can see in the Developer tools what is passed in the request? What gives var_dump($_POST); ?

  • 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 !

  • array(0) { } gave nothing..

  • I took e.preventDefault(); and started sending to the database but gives refresh on the page without it .. I do not understand..:(

  • 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 line var data = $(this).serialize();. What gives?

  • Whenever I try to send something give me this error: http://i.imgur.com/sOMQK5z.png but nothing ever happens on the page..

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.