admin管理员组

文章数量:1355602

I have to make a calculation with Celsius (C) and Fahrenheit (F), and I think the form action line is wrong. How can I fix it?

Here is my HTML:

<form action="javascript.html">
    Convert Celsius to Fahrenheit:<br>
    <input type="text" name="converterCtoF"><br>
    <input type="submit" value="Submit">
</form>

Here is my JavaScript code:

function tempertureConverter(C) {
    return (9/5)*C + 32;
}

What about the other way as well? Converting F to C.

I have to make a calculation with Celsius (C) and Fahrenheit (F), and I think the form action line is wrong. How can I fix it?

Here is my HTML:

<form action="javascript.html">
    Convert Celsius to Fahrenheit:<br>
    <input type="text" name="converterCtoF"><br>
    <input type="submit" value="Submit">
</form>

Here is my JavaScript code:

function tempertureConverter(C) {
    return (9/5)*C + 32;
}

What about the other way as well? Converting F to C.

Share Improve this question edited Jul 20, 2020 at 15:45 Peter Mortensen 31.6k22 gold badges110 silver badges133 bronze badges asked Nov 9, 2013 at 20:35 HotSauceyHotSaucey 1451 gold badge1 silver badge6 bronze badges 1
  • You might want to check the solution at: stackoverflow./questions/133925/… – Joseandro Luiz Commented Nov 9, 2013 at 20:50
Add a ment  | 

4 Answers 4

Reset to default 3

You should be posting to a different file with your form, but since you didn't specify the mark-up for the page you are posting to, here is a solution that will display the Fahrenheit on the current page.

<script>
    function tempertureConverter(){
        var C = document.getElementById("degree").value;
        alert((9/5)*C+32);
    }
</script>

<form>
    Convert Celsius to Fahrenheit:<br>
    <input type="text" name="converterCtoF" id = "degree"><br>
    <input type="button" value="Submit" id = "submit"
           onClick = "tempertureConverter()">
</form>

The above grabs the Celsius value the user inputs and runs the conversion function on it. The result is then displayed as a pop-up in the browser.

A working example is here.

Is this jfiddle what you had in mind? It listens for the form submission and calculates the Fahrenheit from the Celsius text field. This would all be on one page.

HTML:

<form name="temperature_form" action="">
    Convert Celsius to Fahrenheit:<br>
    <input type="text" name="converterCtoF" id="converterCtoF"><br>
    <input type="submit" value="Submit">
</form>

JavaScript:

document.forms["temperature_form"].onsubmit = function(){
    var c = document.getElementById("converterCtoF").value;
    var f = (9/5)*c + 32;
    alert(f);
    return false;
}

I think you want to set the form action to your PHP script, not JavaScript.

<form action="celsius.php" method="get">
    Convert Celsius to Fahrenheit:<br>
    <input type="text" name="converterCtoF"><br>
    <input type="submit" value="Submit">
</form>

Then in celsius.php you can access to the text field value via $_GET['converterCtoF'].

You would not change a PHP script. You need to create one. In this example above ...

<form action="celsius.php" method="get">
    Convert Celsius to Fahrenheit:<br>
    <input type="text" name="converterCtoF"><br>
    <input type="submit" value="Submit">
</form>

You need to create a file named celcius.php and add something similar to:

<?php
  $c = $_GET['converterCtoF'];
  $f = convertTemp(c);

  echo(f);

  function convertTemp(temp) {
    return (9/5)*C + 32;
  }
?>

本文标签: How do you link an HTML form to a function in JavaScriptStack Overflow