-1

I am trying to take two numbers in an html form and upon hitting the classic 'submit' button, do a little math behind the scenes with javascript and then show the results on the web page.

I can't use php with my web host.

Ihis is what I have so far:

URL

The code so far:

var x = dirtyfiat;
var y = price;
var z = x / (y / 100000000);
document.getElementById("sats").innerHTML = z;
<form>
  How much #dirtyFiat U.S. Dollars?
  <input type="float" name="dirtyfiat"> What is the price of bitcoin?
  <input type="float" name="price">
  <input type="submit" value="Let's go!">
</form>

<p id="sats"></p>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • in the URL given above, i fixed the variables to: x = 1, and y = 8600. just to see and know the page will print the calculation. – luggagedonkey Jan 21 '20 at 09:03
  • 5
    I don't know where to start ... By no means to be offending, but everything is wrong, you really have to get familiar with the basics of how a HTML page, and especially form element, are working ... For starters, submit button submits the form to your server, it doesn't execute your script. Also referencing elements on the page directly using their name is not reliable. You need to drop the submit button, and add a button instead, which on you'll listen on click events, and then execute some scripit when that button is clicked. – Teemu Jan 21 '20 at 09:05
  • 1
    i won't be offended by any comments. i just want help in getting the page to work. my web host allows me to input custom HTML and i've gotten the page to work to a certain degree. i only get offended by people who get offended. – luggagedonkey Jan 21 '20 at 09:10
  • i spent time learning rudimentary php, because the expectation was that this calculation would be straight forward, then learned very abruptly that my web host doesn't support php, but they do support javascript. so that's how i landed here. thanks guys. – luggagedonkey Jan 21 '20 at 09:13
  • What's the exact problem here? Anything not working with all that code? Please add your expectations and possible error messages to the question by editing it – Nico Haase Jan 21 '20 at 13:02
  • final page published just now: https://www.windshieldtime.studio/satoshimath – luggagedonkey Jan 21 '20 at 23:38
  • as of now, at my host, everything works perfectly. thanks again to Michel @mplungjan :) – luggagedonkey Jan 21 '20 at 23:47

2 Answers2

2
  • You need to wrap the code in a function. The function needs to be executed on click of the submit
  • The form submit event has to have a preventDefault or the button needs to be type="button"
  • The dirtyfiat and price needs to be taken from the fields using document.querySelector("[name=dirtyfiat]").value or give them an ID which is simpler
  • HTML does not (yet) have a float type. You need "number" Is there a float input type in HTML5?

  • You also need some handling of Not A Number and division by 0

window.addEventListener("load",function() { // on page load
  document.getElementById("myForm").addEventListener("submit",function(e) { // on submit
    e.preventDefault(); // stop submission
    // get values - the + converts a string containing a number to a number
    var x = +document.getElementById("dirtyfiat").value || 0; // if nothing entered make it 0
    var y = +document.getElementById("price").value || 0; // if nothing entered make it 0
    if (isNaN(x)) x = 0;
    if (isNaN(y)) y = 0;
    document.getElementById("sats").innerHTML = (y !=0)   ? x / (y / 100000000) : "Cannot divide by 0";
  })    
})
<form id="myForm">
  How much #dirtyFiat U.S. Dollars?
  <input type="number" id="dirtyfiat" name="dirtyfiat"> What is the price of bitcoin?
  <input type="number" id="price" name="price">
  <input type="submit" value="Let's go!">
</form>

<p id="sats"></p>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • OMG. that is beautiful. works here perfectly. copy+paste to my web host didn't work. probably something they don't support again. – luggagedonkey Jan 21 '20 at 09:21
  • Should work anywhere. If you paste my form instead of your form (I added an ID) and my script inside your script tags, then it should work – mplungjan Jan 21 '20 at 09:24
  • thanks so much! i love the Dutch. there is an extremely prominent figure in the bitcoin space. his name is "plan B". we all love his quant work. THANK YOU! – luggagedonkey Jan 21 '20 at 09:27
  • https://twitter.com/100trillionUSD – luggagedonkey Jan 21 '20 at 09:28
  • I am Danish ;) Just live in Holland – mplungjan Jan 21 '20 at 09:30
  • 1
    whoops, sorry :) honest mistake. we interviewed the CTO of OfferUp, he is Dutch and gave us some cutlture/geography lesson. that show is not out yet. – luggagedonkey Jan 21 '20 at 09:31
  • I updated the description of the answer – mplungjan Jan 21 '20 at 09:37
  • I'm leaving my answer. Yours is overkill for the question, and it includes a couple concepts that a beginner likely won't understand. Solid answer, though. – Decipher Me Please Jan 21 '20 at 09:41
  • Curious what concepts are more confusing to a beginner, both our use of eventListeners or the lack of handling the default behaviour of such an eventListener and attaching it to a dubious element. – mplungjan Jan 21 '20 at 10:05
  • i will likely consider myself a beginner forever...so everything is confusing. thank you again Michel @mplungjan - your help was perfect! – luggagedonkey Jan 21 '20 at 23:49
0

First, "float" is not a valid input type. You can find a list of them here:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input

We'll change those to number instead. Next, we need an id for each input so we can target them.

Here is the corrected HTML:

<form>
  How much #dirtyFiat U.S. Dollars?
  <input id='dirtyinput' type="number" name="dirtyfiat"> What is the price of bitcoin?
  <input id='priceinput' type="number" name="price">
  <input id='submit' type="submit" value="Let's go!">

</form>
<p id="sats"></p>

For the javascript, first reference the ids with document.getElementById() and store those ids in variables. Next, assign the result of the math to a variable.Next, we save the the button id to a variable. Then, add the event listener to the submit button. Then, we call the

Here is the corrected JS:

var output = document.getElementById('submit-btn');
output.addEventListener('click', function() {
    var x = document.getElementById('dirtyinput').value;
    var y = document.getElementById('priceinput').value;
    var result = x / y / 100000000;
    document.getElementById("sats").innerHTML = result;
})

This is the basic version of what you requested. There is a lot more you can do with error handling and value checking.