Search code examples
htmlurluser-input

How to get userinput from a form into a variable


I created an easy tool on a windows pc(batch-file shown below), but it also needs to work on smartphones, hense i switched to html. It should open a predifined URL(website) with a userdefined phonenumber. I get stuck at the part where the userdefined Phonenumber entered in a form, should be read and put in a variable and on clicking a button the predefined website should open with the userdefined phonenumber added to the URL.

<form>
      <label for="Pnumber">Mobile Phonenumber:</label><br>
      <p> 06- <input type="var" id="Pnumber" name="Pnumber"></p><br>
</form>
      <button onclick="window.location.href='https://example.com/'+Pnumber;"> Open Chat!
</button>

Batchfile that did the job, that looks like this:

SET /P phonenumber=Enter the phonenumber:
explorer https://example.com/%phonenumber%

Can somebody tell me how i can extract the phonenumber entered by the user, onclick, out of the form and be added to the URL(in this case example.com/)

Some help would be appreciated!

When i predefined variable and added it to the URL, it worked/

<head><script> var Phonenumber = 31612345678; </script> </head>
<body>
    <input type="button" onclick="location.href='https://example.com'+Phonenumber;" value="URL + Phonenumber" />
</body>

Solution

  • You should use document.getElementById() function to get value of Pnumber id.

    <form>
      <label for="Pnumber">Mobile Phonenumber:</label><br>
      <p> 06- <input type="text" id="Pnumber" name="Pnumber"></p><br>
    </form>
    <button onclick="window.location.href='https://example.com/' + 
    document.getElementById('Pnumber').value;"> Open Chat!</button>