-1

I need to use forms on my first html page to collect data for use in running a javascript function that calculates the distance formula like so -

Distance Formula

In any case, I'm having difficulty writing the function that can calculate this formula. All the examples I can find online either don't involve a 3rd variable (the Z's) or use latitudes and longitudes. Additionally, many of these examples do the input and calculation on 1 page, whereas I need to take the input from page 1 (the values for the variables) and use it as input for the script in page 2.

Here is what I have so far (page 1) --->

    Distance Formula - Input Values for X1, Y1, Z1 and X2, Y2, Z2. <form name="distance"> 
  X1: <input type="text" name="x1_val" size="5">
    <br>
  Y1: <input type="text" name="y1_val" size="5">
    <br>
  Z1: <input type="text" name="z1_val" size="5">
    <br>
  X2: <input type="text" name="x2_val" size="5">
    <br>
  Y2: <input type="text" name="y2_val" size="5">
    <br>
  Z2: <input type="text" name="z2_val" size="5">
    <br>

  <input type="submit" value="Submit">

</form>

Can anyone help me with the script part for the formula? As well as how to take the data from the first page using forms and use it as input for the Js script function in the second? (Perferably without JSON or JQuery if possible)

nwd12a
  • 21
  • 4
  • I downvoted this because this is very basic stuff and you're supposed to do *a lot* more research before asking here. –  Oct 10 '16 at 18:30

2 Answers2

0

As far as I got your question, you need smth like this:

function getDistance(x1, x2, y1, y2, z1, z2) {
    var pow = function(val) {
            return Math.pow(val, 2);
        },
        sqrt = Math.sqrt;
    return sqrt(pow(x2 - x1) + pow(y2 - y1) + pow(z2 - z1))
}
0

Assuming you have the values in variables, do this:

var d1 = x2 - x1;
var d2 = y2 - y1;
var d3 = y3 - y1;
var dist = Math.sqrt(d1*d1 + d2*d2 + d3*d3);

However, the other problem here is passing the values, and you were told how to do this in your previous question. A <form> is usually processed by server-side scripting like PHP; if you want to use JavaScript instead, simply don't move to a second page, just calculate the result and output it on the current page. Like this:

HTML:

<div id="output"></div>

JS:

// calculation here
document.getElementById("output").innerHTML = "Result: " + dist;