That's just not how things work. What happens is
- A user agent -- usually a browser, but here an Arduino -- makes a request to a web server. This request is usually a GET, but could be a POST, DELETE, etc
- The web server may respond with HTML
- The user agent will parse this HTML, which in this case contains at least one
<form>
and some<script>
, which are converted into the corresponding things in the DOM and thefunction
in JavaScript - The user agent will trigger the function due to the user interaction with the
<button>
You can't trigger JavaScript in a page if you don't have a JavaScript engine, for a page you did not receive and parse to build a DOM.
But based on what the sb
function actually does, you might be able to do the equivalent without any JavaScript at all.
Look at the HTML and find the first (likely only) <form>
-- first because the JavaScript said form[0]
. Check for two attributes
method
: if it's missing, it defaults toget
, but it might bepost
action
: if it's missing, it's the current page's path, but could be something else starting with/
Possibly it has method="post"
and no action
, in which case you were close. But val
is the variable name in the function in JavaScript and has no relation to what the web server expects. The form element -- between the form[0].
and .value
is answer
, so try sending
POST / HTTP/1.1
Host: 10.105.154.258
Content-Type: application/x-www-form-urlencoded
Content-Length: 8
answer=1
Note the blank line between the headers and the body, which you did not have. And strictly speaking, you don't need a line break after the body with println
-- you say how many bytes to expect -- but it probably won't hurt.
The value of answer
need to be encoded. And if it's a GET instead, the values go in the URL as query parameters.