Reputation: 1
This Html coding with css and php.I am getting an error that you result variable is not declareed.but i had declare it in my form.please check and tell me about this error. calculator
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>
This is Php coding
if(isset($_POST['add'])){
$first_value = $_POST['f_value'];
$sec_value = $_POST['s_value'];
//--calculation variables---//
$result = $first_value + $sec_value;
}
?>
</head>
Html form starts from here
<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>
Upvotes: 0
Views: 142
Reputation: 2168
Your $result
is falling out of scope:
<?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' >
To solve this problem, first declare $result within the same scope as you intend to echo it:
<?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' >
Futher information about variable scope
Upvotes: 0
Reputation: 11984
if(isset($_POST['add'])){
$first_value = $_POST['f_value'];
$sec_value = $_POST['s_value'];
$result = $first_value + $sec_value;
}
else{
$result= '';
}
Upvotes: 0
Reputation: 30488
Use isset
here
<input type="text" maxlength="50" value="<?php if(isset($result)) { echo $result; } ?>" Name='result' >
Upvotes: 1