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
4 Answers
Reset to default 3You 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
版权声明:本文标题:How do you link an HTML form to a function in JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744020386a2577070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论