-5

I'm sorry to make this question, as others have done it already. But mostly don't have a good explanation, or always are mixed with more complex chats functions. I want a simple code. The only thing i want is to make my textarea value submit and get inserted to my db using ENTER key. Please don't redirect me to another question, as i know they must be others with starter skills that wants to learn. Just adjust the code to the simple form i have added. Thanks.

Code:

<?php 
if(isset($_POST['submit'])) {
$comment = $_POST['textarea'];
$db->query("INSERT INTO blog(textarea) VALUES('$comment')");
}
?>
<form id="form1">
<div>
Comment:
</div>
<div>
<textarea name="textarea" form="form1" maxlength="200" id="textarea" placeholder="Make your comment..."></textarea>
<input style="visibility:hidden" type="submit" form="form1" name="submit" id="submit" value="Submit"> 
</div>
</form>
user3672191
  • 1
  • 1
  • 5
  • Why not use `` instead of ` – Joeytje50 May 25 '14 at 20:53
  • i was thinking about using text, but as fb is using textarea, i would like to try with textarea. – user3672191 May 25 '14 at 20:54
  • 2
    The point of a textarea is that it supports multiple lines. How are you going to insert a new line if enter submits the form? – Quentin May 25 '14 at 20:55
  • 4
    **Danger**: You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that you need to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin May 25 '14 at 20:55
  • [The HTML5 placeholder attribute is not a substitute for the label element](http://www.456bereastreet.com/archive/201204/the_html5_placeholder_attribute_is_not_a_substitute_for_the_label_element/) – Quentin May 25 '14 at 20:55
  • @Quentin I just thought of this: Stackoverflow comments are also made in a textarea, just because that allows word-wrapping. – Joeytje50 May 25 '14 at 20:59

1 Answers1

0

You can do this quite simply by adding a keypress event handler to the textarea:

<form id="form1">
    <div>
        Comment:
    </div>
    <div>
        <textarea onkeypress="if(event.which==13)document.getElementById('form1').submit();"
            placeholder="Make your comment..."
            name="textarea" form="form1" maxlength="200" id="textarea"></textarea>
        <input style="visibility:hidden" type="submit" form="form1" name="submitForm" id="submitForm" value="Submit"> 
    </div>
</form>

That checks if the pressed key has keycode 13 (which is the keycode for the enter key), and submits the form if it does.

Joeytje50
  • 18,636
  • 15
  • 63
  • 95
  • It wont work, it wont submit. – user3672191 May 25 '14 at 21:04
  • @user3672191 do you even have a submit action defined for your form? The form doesn't even have an `action` attribute, so what is the form supposed to do? – Joeytje50 May 25 '14 at 21:04
  • Fun-fact: Even the specs say the action attribute is required, it's optional in most browsers on earth since ages. – hakre May 25 '14 at 21:08
  • @hakre yeah, but if there's no `action` attribute, *and* no `submit` event handler, what's the `
    ` tag even for? It serves no purpose.
    – Joeytje50 May 25 '14 at 21:10
  • @Joeytje50: Without an action attribute, the action location is a relative URI as zero-length string. That is pointing to the base-URI as form-action (which normally is the same page). A submit handler is not necessary, it's additional client-side scripting, but the browser can work without. – hakre May 25 '14 at 21:12
  • @user3672191 it should work now. The `` with `id="submit"` was interfering with the form's `.submit()` method. – Joeytje50 May 25 '14 at 21:16
  • @Joeytje50, the form will submit, but the php wont run. – user3672191 May 26 '14 at 20:47
  • @user3672191 then there must be a problem with your PHP. If you can't figure out what's going wrong, you should post it as a seperate question, as I wouldn't know what's going on then. – Joeytje50 May 26 '14 at 21:10
  • Got it fixed. I needed to use if($_SERVER['REQUEST_METHOD'] == 'POST') instead of if($_POST['submit']). – user3672191 May 27 '14 at 00:07