[英]set value of input field by php variable's value
I have a simple php calculator which code is:我有一个简单的 php 计算器,其代码是:
<html>
<head>
<title>PHP calculator</title>
</head>
<body bgcolor="orange">
<h1 align="center">This is PHP Calculator</h1>
<center>
<form method="post" action="phptest.php">
Type Value 1:<br><input type="text" name="value1"><br>
Type value 2:<br><input type="text" name="value2"><br>
Operator:<br><input type="text" name="sign"><br>
Result:<br><input type"text" name="result">
<div align="center">
<input type="submit" name="submit" value="Submit">
</div>
</form>
</center>
<?php
if(isset($_POST['submit'])){
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
if($value1=='') {
echo "<script>alert('Please Enter Value 1')</script>";
exit();
}
if($value2=='') {
echo "<script>alert('Please Enter Value 2')</script>";
exit();
}
if($sign=='+') {
echo "Your answer is: " , $value1+$value2;
exit();
}
if($sign=='-') {
echo "Your answer is: " , $value1-$value2;
exit();
}
if($sign=='*') {
echo "Your answer is: " , $value1*$value2;
exit();
}
if($sign=='/') {
echo "Your answer is: " , $value1/$value2;
exit();
}
}
?>
All I want to do is that answer should be displayed in the result input field instead of echoing them separately.我想要做的就是答案应该显示在结果输入字段中,而不是单独回显它们。 Please help?
请帮忙? I Know it's simple but I am new in PHP.
我知道这很简单,但我是 PHP 新手。
One way to do it will be to move all the php code above the HTML, copy the result to a variable and then add the result in the <input>
tag.一种方法是将所有 php 代码移动到 HTML 上方,将结果复制到变量中,然后将结果添加到
<input>
标签中。
Try this -尝试这个 -
<?php
//Adding the php to the top.
if(isset($_POST['submit']))
{
$value1=$_POST['value1'];
$value2=$_POST['value2'];
$sign=$_POST['sign'];
...
//Adding to $result variable
if($sign=='-') {
$result = $value1-$value2;
}
//Rest of your code...
}
?>
<html>
<!--Rest of your tags...-->
Result:<br><input type"text" name="result" value = "<?php echo (isset($result))?$result:'';?>">
inside the Form, You can use this code.在表单中,您可以使用此代码。 Replace your variable name (i use $variable)
替换您的变量名(我使用 $variable)
<input type="text" value="<?php echo (isset($variable))?$variable:'';?>">
尝试这个
<input class="qtytext-box" type="number" value= <?php echo $colll2; ?> >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.