0

这个带有 css 和 php 的 Html 编码。我收到一个错误,你的结果变量没有被声明。但我已经在我的表单中声明了它。请检查并告诉我这个错误。计算器

this style .css file


        <style>
        form {
    display:block; 
    background-color: #333399;
    width:300px;
    height:500px;
    border:thick;
    border: #330000;

    color: #FFCC00;
    }


h1 {
    text-align:center;
    z-index: 2px;
    }
    </style>

这是php编码

if(isset($_POST['add'])){
    $first_value = $_POST['f_value'];
    $sec_value = $_POST['s_value'];

    //--calculation variables---//

         $result = $first_value + $sec_value;           


        }
        ?>  

    </head>

html表单从这里开始

<body>
        <form  method="post" action="new.php" name="calculator">

    &nbsp;<h1> calculator</h1> 
    <p>
    <strong>Frsit value</strong>&nbsp;&nbsp;&nbsp;&nbsp;
    <input  type="text" name="f_value" >

    <p><strong>Second value</strong> <input type="text" name="s_value" maxlength="50">
    <p>
    &nbsp;<input name="add" type="submit" value="add" >
    <!--&nbsp;<input name="sub" type="submit" value="sub">
    &nbsp;<input name="sub" type="submit" value="multiply">
    &nbsp;<input name="sub" type="submit" value="divide">-->
    `enter code here`<p>

    <h2 style="border:thick">Result
      <input type="text" maxlength="50" value="<?php echo $result ; ?>" Name='result' >
    </h2>


        </form>

</body>

</html>
4

3 回答 3

1

isset在这里使用

<input type="text" maxlength="50" value="<?php if(isset($result)) { echo $result; } ?>" Name='result' >
于 2013-04-17T12:20:10.163 回答
0
if(isset($_POST['add'])){
    $first_value = $_POST['f_value'];
    $sec_value = $_POST['s_value'];
    $result = $first_value + $sec_value;  
}
else{
    $result= '';
}
于 2013-04-17T12:22:48.183 回答
0

$result的超出范围:

<?php
if(isset($_POST['add'])) {  //scope begins here
  //php omitted for brevity

  //$result is declared within this scope
  $result = $first_value + $sec_value;

} // scope ends here - after this point, $result no longer exists!
?>
<!-- html omitted for brevity -->
<!-- This is OUTSIDE the scope where $result was declared - we can't get it any more! -->
<input type="text" maxlength="50" value="<?php echo $result ; ?>" Name='result' >

为了解决这个问题,首先在你打算回显它的相同范围内声明 $result:

<?php
$result = 0;

if(isset($_POST['add'])) {
  //php omitted for brevity

  //change $result's value
  $result = $first_value + $sec_value;
}
?>
<!-- html omitted for brevity -->
<input type="text" maxlength="50" value="<?php echo $result ; ?>" Name='result' >

有关变量范围的更多信息

于 2013-04-17T12:26:14.560 回答