# PHP, textarea and submit button. enter/return default



## firestormer (Sep 16, 2005)

```
[SIZE="3"]		<textarea cols="25" rows="3" name="message" maxlength="<?php 

echo $chatbox_config['max_msg_len']; ?>" value="Enter Message" 

onFocus="clear_text()" <?php if (isset($chatbox_config['vietuni'])) echo " 

onkeyup='initTyper(this);'"; ?> class="editbox"> </textarea>
		<input type="hidden" name="sent" value="">
		<input type="submit" name="submit_button" value="<?php echo 

$lang['Send']; ?>" class="button"><br />&nbsp;<br />
		<a href="javascript:void(0);" onclick="window.open

('../posting.php?mode=smilies', '_chatboxsmilies', 

'HEIGHT=450,resizable=yes,scrollbars=yes,WIDTH=700');">Smilies Box</a>[/SIZE]
```
This forms the lower part of a chatbox. see bellow:









The problem is when typing in the text area box preesing enter will start a new line. I dont want this. By pressing enter the submit button needs to be pressed and the text submited.

How to i change this?

Thanks


----------



## Menekali (Sep 6, 2007)

This should work you for you:


```
<textarea onkeypress="if (event.keyCode == 13) {this.form.submit();}">
```


----------



## firestormer (Sep 16, 2005)

Thanks ill give it a go.


----------



## firestormer (Sep 16, 2005)

Do i need to make any changes to the code you gave, as it is all it does is refresh the page when i hit enter and not send the message. I have attatched the full php script as a txt rather then php.


----------



## firestormer (Sep 16, 2005)

by using this code:


```
<textarea onkeypress="if (event.keyCode == 13) submit();">
```
The message that was previously entered is sent and not the message that has just been typed.

From past experience with VB this is because the focus hasn't shifted of the textbox and thus the value of the textbox hasn't been updated.

Unfortunatly i dont know how to reslove this in php. In VB id simply use some code to shift the focus to another object before submiting.


----------



## ehymel (Aug 12, 2007)

firestormer said:


> From past experience with VB this is because the focus hasn't shifted of the textbox and thus the value of the textbox hasn't been updated.
> 
> Unfortunatly i dont know how to reslove this in php. In VB id simply use some code to shift the focus to another object before submiting.


In this case the submit() function is javascript, so php versus VB is not relevant. The original poster needs to concentrate on the javascript portion for event handling.

A quick google search yielded this page, which looks promising: http://www.webdeveloper.com/forum/archive/index.php/t-52668.html


----------

