Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将字段值设置为计算结果_Php_Html_Forms - Fatal编程技术网

Php 如何将字段值设置为计算结果

Php 如何将字段值设置为计算结果,php,html,forms,Php,Html,Forms,我有一个表单,它有一个值来自php计算的字段。计算只是涉及纯整数的基本算术。但有一种情况下,该值将为零。因此,表单假定该字段为空 我怎么能绕过那个? 下面是一个代码示例 if ( isset($_POST['input1']) && isset($_POST['input2']) && isset($_POST['ans']) ) { // CHECKING IF ANY OF THE FIELDS WERE LEF

我有一个表单,它有一个值来自php计算的字段。计算只是涉及纯整数的基本算术。但有一种情况下,该值将为零。因此,表单假定该字段为空

我怎么能绕过那个? 下面是一个代码示例

if ( 
    isset($_POST['input1']) &&
    isset($_POST['input2']) &&
    isset($_POST['ans'])
    )
{   
    // CHECKING IF ANY OF THE FIELDS WERE LEFT EMPTY
    $input1 = $_POST['input1'];
    $input2 = $_POST['input2'];
    $ans = ($input1)-($input2);
    $ans_set = 0;

    if ($ans === $ans_set ) {
        # code...
        //echo "Same";
        $ans = '0';
    } else {
        # code...
        //echo "Different";
        echo "Check data types";
    }


    if ( 
        !empty($input1) &&
        !empty($input2) &&
        !empty($ans) 
         )
    {   
        $query = "INSERT INTO formaths VALUES ( 'NUll', NOW(),
                        '".mysql_real_escape_string($input1)."',
                        '".mysql_real_escape_string($input2)."',
                        '".mysql_real_escape_string($ans)."' )";

        $query_run = mysql_query($query);
                    if ( $query_run )
                    {
                        //header("Location:". __DIR__."../registration_success.php");
                        echo "<p class='echo'>Data entry was successful.</p>";
                    }
                    else
                    {
                        echo "<p class='echo'>It seems that we couldn't save that at this time.</p>";
                    }

    }
    else
    {
        echo "<p class='echo'>Please make sure all fields are filled and are correct.<br/>You must fill in the area field last!</p>";
    }
}

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <form action="formaths.php" method="POST">
        <input name="input1" type="text" autofocus/>
        <input name="input2" type="text"/>
        <input name="ans" type="text" value="<?php if (isset($ans)) { echo $ans; }?>" readonly />

        <input type="submit" value="DO IT" />
    </form>
    enter code here

</body>
</html>
if(
isset($\u POST['input1']))&&
isset($_POST['input2']))&&
isset($_POST['ans']))
)
{   
//检查是否有任何字段留空
$input1=$_POST['input1'];
$input2=$_POST['input2'];
$ans=($input1)-($input2);
$ans_set=0;
如果($ans===$ans\U集){
#代码。。。
//呼应“相同”;
$ans='0';
}否则{
#代码。。。
//呼应“不同”;
回显“检查数据类型”;
}
如果(
!空($input1)&&
!空($input2)&&
!空($ans)
)
{   
$query=“插入格式值('NUll',NOW(),
“.mysql\u real\u escape\u字符串($input1)。”,
“.mysql\u real\u escape\u字符串($input2)。”,
“.mysql\u real\u escape\u字符串($ans)。”);
$query\u run=mysql\u query($query);
如果($query\u run)
{
//标题(“位置:”../registration\u success.php”);
echo“

数据输入成功。

”; } 其他的 { echo“

现在我们似乎无法保存它。

”; } } 其他的 { echo“

请确保所有字段都已填写且正确无误。
您必须最后填写区域字段!

”; } } ?>
我看不到任何代码,但我认为
intval()
floatval()
会对您有所帮助。您的倒数第二句没有意义。您的意思是希望表单在计算为零时将字段视为空?如果在回答值为
0
时仍希望
INSERT
,则应仅检查
input1
input2
是否为空。