Emil Elkjær
Emil Elkjær

Reputation: 685

jQuery - doesnt submit data on enter

I am trying to work with a chatroom and some users would like a feature to submit on enter.

I current have this code:

Form html:

<form id="send-message-area" name="send-message-area" method="post" action="">
    <textarea id="sendie" name="sendie" maxlength = '255'></textarea>
    <input type="submit" name="sendieButton" id="sendieButton" value="Send" />
</form>

jQuery:

$(document).ready(function(){ 

$("textarea").keyup(function(event){
    if(event.keyCode == 13){
        //$("form").submit();
        $("form").trigger("submit");
    }
});

});

However when hitting enter it does indeed submit, however it doesn't send any data with it. It works just fine when pressing the submit button.

I already tried $("form").submit(); but it does the exact same.

EDIT:

I think the problem lays in my PHP.

if(isset($_POST['sendieButton'])){

    $fromID = $brugernavn;
    $fromMsg = $_POST['sendie'];

    sendMsg($fromID, $fromMsg);

};

However when changing to check for $_POST['send-message-area'] it doesn't work at all.

Upvotes: 0

Views: 63

Answers (2)

Nehal
Nehal

Reputation: 1022

Your button value will not be submitted until you click on it. So either you trigger its click event

$(document).ready(function(){ 

    $("textarea").keyup(function(event){
        if(event.keyCode == 13){
            //$("form").submit();
            $("#sendieButton").trigger("click");
        }
    });

});

or check only textarea value isset or not in PHP

if(isset($_POST['sendie'])){

    $fromID = $brugernavn;
    $fromMsg = $_POST['sendie'];

    sendMsg($fromID, $fromMsg);
};

Upvotes: 2

Molda
Molda

Reputation: 5704

You need to check for name of the textarea not the button

if (isset($_POST["sendie"])) {
    $fromMsg = $_POST['sendie'];   
}else{  
    echo "no sendie";
}

Upvotes: 0

Related Questions