-4

I have a form:

<form method="post">
<input type="text" name="username" value="Username" />
<input type="submit" name="go" value="GO!" />
</form>

And when the user clicks on the submit button, the page gets the fields' details with:

if(isset($_REQUEST['go'])){

        $username = $_POST['username'];
        echo "Hello" . $username . "";
}

How could I do this, when the user finishes writting, without clicking the button?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Luis Parker
  • 131
  • 2
  • 2
  • 8
  • It's not clear what you're asking. What does `$_REQUEST` have to do with "showing a result" or with "user hits ENTER" in any way? Can you be specific about what you're trying to accomplish and how you're trying to accomplish it? – David Jun 12 '13 at 17:01
  • Wait I am going to edit topic, sorry! – Luis Parker Jun 12 '13 at 17:07
  • Done, see again please. – Luis Parker Jun 12 '13 at 17:11
  • Are you just asking how to submit a form when the user presses enter? If so, this has nothing to do with PHP, it's entirely client-side. And it's been answered _many_ times on the internet. Take a look here: http://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed – David Jun 12 '13 at 17:14
  • No, no when the user finishes writting, please read the topic – Luis Parker Jun 12 '13 at 17:16

1 Answers1

1

First of all, how do you define "when the user finishes writing"?

If you're just looking to echo the contents of the input to another element, you can do that as-the-user-types by binding to the keyup event. Using jQuery it might look something like this:

$('input[name="username"]').keyup(function (e) {
    $('#output').text($(this).val());
});

What this would do is any time a key is pressed in the input of name "username", the text in an element of id "output" would be updated with the current value of that input. So, of course, you'd need some kind of element with an id of "output":

<span id="output"></span>

If you don't want this to happen as-they-type but instead want to wait until the user presses the return key, you'd still use the same event. You'd just check which key was pressed in the event and only update the text if it was the return key:

$('input[name="username"]').keyup(function (e) {
    if (e.keyCode == 13) {
        $('#output').text($(this).val());
    }
});
David
  • 208,112
  • 36
  • 198
  • 279
  • 1
    i think the main point to be taken away from this answer is that if you want an element updated without having to "submit" anything, you need to use client-side scripting (such as jQuery as you point out) rather than a server-side language like php – Jeff Hawthorne Jun 12 '13 at 17:59