PHP平方根计算器(带有HTML)

用户名

我试图使用表格使用PHP和HTML制作平方根计算器。但它似乎无法获得输出语句。这里是:

    <?php
$num = $_POST['getroot'];
$pull_sqrt = print(sqrt($num));
print("The Square root of "$num"is "$pull_sqrt);
?>

<form action="root.php" method="post">
<input type="text" id="getroot" value="Number"/>
<input type="submit" id="submitroot" value="Calculate"/>
</form>

对于root.php:

<?php
$num = $_POST['getroot'];
$pull_sqrt = print(sqrt($num));
print("The Square root of "$num"is "$pull_sqrt);
?>

请帮我解释一下,我仍然不知道PHP是否允许sqrt();。作为功​​能了。任何重新编辑的方式都可以,我想提供一种解决此问题的解释方法。谢谢!

放克四十尼纳

您没有名为的表单元素 getroot

你想做 <input type="text" id="getroot" name="getroot" value="Number"/>

你不能仅仅依靠一个idPOST需要一个“命名”元素。

您还缺少以下的串联 print("The Square root of "$num"is "$pull_sqrt);

旁注:print从中删除$pull_sqrt = print(sqrt($num));否则它将回显为1

print("The Square root of " . $num . "is " .$pull_sqrt);

由于您是在一页中使用它,因此您需要使用isset()和使用action=""

<?php

if(isset($_POST['submit'])){
$num = $_POST['getroot'];
$pull_sqrt = sqrt($num);
print("The Square root of " . $num  . " is " . $pull_sqrt);

}
?>

<form action="" method="post">
<input type="text" id="getroot" name="getroot"  placeholder="Enter a number"/>
<input type="submit" name="submit" id="submitroot" value="Calculate"/>
</form>

您也可以使用来检查它是否实际上是已输入的数字is_numeric()

<?php 

if(isset($_POST['submit'])){

    if(is_numeric($_POST['getroot'])){
      $num = (int)$_POST['getroot'];
      $pull_sqrt = sqrt($num);
      print("The Square root of " . $num  . " is " . $pull_sqrt);

// Yes, you can do the following:
$pull_sqrt = print($num * $num); // added as per a comment you left, but deleted.
}

else{
echo "You did not enter a number.";
}

}
?>

<form action="" method="post">
<input type="text" id="getroot" name="getroot"  placeholder="Enter a number"/>
<input type="submit" name="submit" id="submitroot" value="Calculate"/>
</form>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章