experchange > javascript

RG III (11-27-19, 09:56 PM)
I've been away from HTML/JavaScript/PHP coding for awhile, so I don't know how much has changed in the web coding world... I'm just curious, but are there any HTML or JavaScript methods for getting user input without using input forms or PHP back-end coding?

-Robert
Martin Leese (11-27-19, 10:34 PM)
RG III wrote:
> I've been away from HTML/JavaScript/PHP coding for awhile, so I don't know how much has changed in the web coding world... I'm just curious, but are there any HTML or JavaScript methods for getting user input without using input forms or PHP back-end coding?


If this was a PHP group, such as
comp.lang.php, then I would have asked how
you do that with "PHP back-end coding?".
However as it isn't, I wont.
Robert Heller (11-27-19, 11:09 PM)
At Wed, 27 Nov 2019 11:56:40 -0800 (PST) RG III <mob.rrr.3> wrote:

> I've been away from HTML/JavaScript/PHP coding for awhile, so I don't know =
> how much has changed in the web coding world... I'm just curious, but are t=
> here any HTML or JavaScript methods for getting user input without using in=
> put forms or PHP back-end coding?


If the back end (server) needs to process the input, then you need some
back-end coding. No reason it has to be PHP. There are other active server
processing technologies.

Getting input from the user pretty much needs at least <input .../>, <select
...> or <textarea ...> tags. Whether or not they are in a <form...> or not
depends...
Michael Haufe (TNO) (11-28-19, 12:49 AM)
On Wednesday, November 27, 2019 at 1:56:48 PM UTC-6, RG III wrote:
> I've been away from HTML/JavaScript/PHP coding for awhile, so I don't know how much has changed in the web coding world... I'm just curious, but arethere any HTML or JavaScript methods for getting user input without using input forms or PHP back-end coding?


Some context would be helpful otherwise a trivial example is:

```
let result = window.prompt("Enter Age",18)
```

And that functionality has existed as long as I can remember...
JJ (11-28-19, 06:44 PM)
On Wed, 27 Nov 2019 11:56:40 -0800 (PST), RG III wrote:
> I've been away from HTML/JavaScript/PHP coding for awhile, so I don't know
> how much has changed in the web coding world... I'm just curious, but are
> there any HTML or JavaScript methods for getting user input without using
> input forms or PHP back-end coding?
> -Robert


If it's just text input, then aside from what already have been mentioned,
additionally, `keypress` or `keydown` event can be used to listen for key
presses. At the proper time, of course. Then send the text to the server
using AJAX.

However, if it's a file input, you'll have to use an INPUT element (of
`file` type - with or without FORM element). There's no other way around it.
Thomas 'PointedEars' Lahn (11-28-19, 07:39 PM)
JJ wrote:

> On Wed, 27 Nov 2019 11:56:40 -0800 (PST), RG III wrote:
> If it's just text input, then aside from what already have been mentioned,
> additionally, `keypress` or `keydown` event can be used to listen for key
> presses. At the proper time, of course. Then send the text to the server
> using AJAX.


This advice is outdated in several respects.

It is better to listen to the “beforeinput” and “input” events, and to the
“keypress” event only as a fallback, because the former are only fired if
something changes without the input control losing focus; and they are also
fired if text is edited using the clipboard (using either a keyboard or a
pointing device), which is not always and everywhere the case with the
“keypress”, “keydown” and “keyup” events.

Notably, the “keydown” event and the (not mentioned, but more appropriate
than that) “keyup” event, and sometimes the “keypress” event, are also fired
if the value of a control does not change, for example if the Backspace key
is pressed in an empty input control or a key is pressed in an
input/textarea control but the maximum number of allowed characters has been
reached (“maxlength” attribute). So using those events in client–server
communication incurs an unnecessary load on the server.

Finally, (outside the jQuery information bubble) nowadays one no longer
speaks of “AJAX” (which once meant “Asynchronous JavaScript and XML”), but
of submitting data or fetching a Web resource asynchronously (via HTTP),
preferably using the Fetch API; where it should be noted that ECMAScript
2018 introduced the “await” operator and “async” keyword as syntactic sugar
for processing the returned promise (no more callbacks for simple tasks):

(async function () {
var response = await fetch('/query?format=json');
var obj = await response.json();
console.log(obj);
}());

<https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event>

<https://developer.mozilla.org/en-US/docs/Web/API/Document/keydown_event>

<https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event>
(deprecated)

<https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await>
Thomas 'PointedEars' Lahn (11-28-19, 07:42 PM)
JJ wrote:

> On Wed, 27 Nov 2019 11:56:40 -0800 (PST), RG III wrote:
> If it's just text input, then aside from what already have been mentioned,
> additionally, `keypress` or `keydown` event can be used to listen for key
> presses. At the proper time, of course. Then send the text to the server
> using AJAX.


This advice is outdated in several respects.

It is better to listen to the “beforeinput” and “input” events, and to the
“keypress” event only as a fallback, because the former are only fired if
something changes without the input control losing focus; and they are also
fired if text is edited using the clipboard (using either a keyboard or a
pointing device), which is not always and everywhere the case with the
“keypress”, “keydown” and “keyup” events.

Notably, the “keydown” event and the (not mentioned, but more appropriate
than that) “keyup” event, and sometimes the “keypress” event, are also fired
if the value of a control does not change, for example if the Backspace key
is pressed in an empty input control or a key is pressed in an
input/textarea control but the maximum number of allowed characters has been
reached (“maxlength” attribute). So using those events in client–server
communication incurs an unnecessary load on the server.

Finally, (outside the jQuery information bubble) nowadays one no longer
speaks of “AJAX” (which once meant “Asynchronous JavaScript and XML”), but
of submitting data or fetching a Web resource asynchronously (via HTTP),
preferably using the Fetch API; where it should be noted that ECMAScript
2018 introduced the “await” operator and “async” keyword as syntactic sugar
for processing the returned promise (no more callbacks for simple tasks):

(async function () {
var response = await fetch('/query?format=json');
var obj = await response.json();

/* executed after the response was received */
console.log(obj);
}());

/* executed before the response is received */
console.log(42);

<https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event>

<https://developer.mozilla.org/en-US/docs/Web/API/Document/keydown_event>

<https://developer.mozilla.org/en-US/docs/Web/API/Document/keypress_event>
(deprecated)

<https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API>

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await>
Arno Welzel (11-29-19, 12:49 PM)
On 27.11.19 20:56, RG III wrote:

> I've been away from HTML/JavaScript/PHP coding for awhile, so I don't
> know how much has changed in the web coding world... I'm just
> curious, but are there any HTML or JavaScript methods for getting
> user input without using input forms or PHP back-end coding?


Yes.
Similar Threads