这个Html编码与css和php.I我得到一个错误,你结果变量未声明。但我已经在我的form.please检查并告诉我有关此错误。 计算器
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">
<h1> calculator</h1>
<p>
<strong>Frsit value</strong>
<input type="text" name="f_value" >
<p><strong>Second value</strong> <input type="text" name="s_value" maxlength="50">
<p>
<input name="add" type="submit" value="add" >
<!-- <input name="sub" type="submit" value="sub">
<input name="sub" type="submit" value="multiply">
<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>
答案 0 :(得分:1)
在这里使用isset
<input type="text" maxlength="50" value="<?php if(isset($result)) { echo $result; } ?>" Name='result' >
答案 1 :(得分:0)
if(isset($_POST['add'])){
$first_value = $_POST['f_value'];
$sec_value = $_POST['s_value'];
$result = $first_value + $sec_value;
}
else{
$result= '';
}
答案 2 :(得分: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' >