首页 > 解决方案 > 将 html 输入作为 php 的变量

问题描述

这是我的第一篇文章。很抱歉,如果我弄错了格式:)

我正在尝试构建一个程序,该程序使用 html 请求输入,将该输入设置为 php 变量,然后计算该变量。除了输入部分,一切都准备好了,我觉得这非常具有挑战性。我的代码如下:

<?php
$x = 1;
$answer = ($x = 1);
if($answer) {
    echo "The answer is: True"; 
} else {
    echo "The answer is: False";    
}
?>

我要设置的变量是 ($x = 1) 部分。代码的目的是查看数学计算是对还是错。该代码已经过测试。

我已经在互联网上搜索了答案,遗憾的是我只看到了不同问题的答案。

标签: phphtmlinput

解决方案


你是这个意思吗?

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <?php
      if (isset($_POST['submit'])) {

        $answer = $_POST['input'];

        if($answer == 1) {
        echo "The answer is: True"; 

        } else {
            echo "The answer is: False";    

        }
    }

    ?>
    <form action="" method="post">
        Enter value: <input name="input" type="text" />
        <input name="submit" type="submit" />
    </form>
</body>

</html>

推荐阅读