Error sending form with JavaScript

0

I am trying to send a form and receive the data that comes from the database through JavaScript but it only sends me a message when I want to send the form and it does not bring anything from the database, and the connection does not it throws me no error

This is how I have the form and I try to send it to the database

 <form method="POST" id="messageForm">
    <textarea name="message" id="" cols="30" rows="10" class="textareaMsg"></textarea>
</form>
<script>

    LoadChat();

    function LoadChat(){
        $.post('../ajax/messages.php?action=getMessages', function(response){
            $('#chat').html(response);
        });
    }

    $('.textareaMsg').keyup(function(event) {
        if (event.which == 13) {
            $('form').submit()
        }
    });

    $('form').submit(function(){
        var message = $('.textareaMsg').val();
        $.post('../ajax/messages.php?action=sendMessages&message='+message, function(response){
            if (response == 1) {
                document.getElmentById('messageForm').reset();

            } else {
                alert("error");
            }
        });

        return false;
    });
</script>

and where I bring the connection and made the querys for the database is as follows

<?php

include "../vista/modulos/dbconnect.php";


//$usuarioActual = $_SESSION["id"];

switch ($_REQUEST['action']) {
    case 'sendMessages':

        $query = $db->prepare("INSERT INTO messages SET message=?");
        $run = $query -> execute([$_REQUEST["message"]]);

        if ($run) {
            echo 1;
            exit;
        }

    break;

    case 'getMessages':

        $stmt = $db->prepare("SELECT * FROM messages");
        $stmt -> execute();
        $rs = $stmt -> fetch();

    break;
}


?>
    
asked by Cesar Gutierrez Davalos 16.07.2018 в 04:53
source

0 answers