在表单字段中发布计算后的php结果
此表单仅接受“inp”字段中的数据。要将两个数字相乘,您必须输入第一个数字,然后点击“=”将数字发送到“输出”字段,然后输入第二个数字并点击“*”。我试图将计算结果($result)发布到“out”字段中,但不确定如何使用php实现这一点。任何帮助都将不胜感激在表单字段中发布计算后的php结果,php,forms,post,Php,Forms,Post,此表单仅接受“inp”字段中的数据。要将两个数字相乘,您必须输入第一个数字,然后点击“=”将数字发送到“输出”字段,然后输入第二个数字并点击“*”。我试图将计算结果($result)发布到“out”字段中,但不确定如何使用php实现这一点。任何帮助都将不胜感激 <body> <form action = "calc.php" method = "post"> <input type="text" value="0.0" name="out"
<body>
<form action = "calc.php" method = "post">
<input type="text" value="0.0" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
php:
<body>
<form action = "calc.php" method = "post">
<input type="text" value="0.0" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
形式:<body>
<form action = "calc.php" method = "post">
<input type="text" value="0.0" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
如果您在同一个文件中编写php和html代码,您可以将简单的写入
输出
输入值计算结果。试着这样做:
<body>
<form action = "calc.php" method = "post">
<input type="text" value="0.0" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
<body>
<form action = "calc.php" method = "post">
<input type="text"
value="<?=!empty($result)?$result:"0.0"?>" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
我修改了你的代码
<body>
<form action = "calc.php" method = "post">
<input type="text" value="0.0" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
$result= '';
if ($_POST) {
$input = (int) $_POST['inp'];
$output= (int) $_POST['out'];
if(isset($_POST['add']) && !empty($input)) {
$result = $output + $input;
//set result to "out"
}
else if(isset($_POST['sub']) && !empty($input)) {
$result = $output - $input;
//set result to "out"
}
else if(isset($_POST['mul']) && !empty($input)) {
$result = $output * $input;
//set result to "out"
}
else if(isset($_POST['div']) && !empty($input)) {
$result = $output / $input;
//set result to "out"
}
else if(isset($_POST['equ']) && !empty($input)) {
$result = $input;
//set result to "out"
} else {
$error = "Please enter a number";
}
}
$result='';
如果(美元邮政){
$input=(int)$_POST['inp'];
$output=(int)$_POST['out'];
如果(isset($_POST['add'])&&&!empty($input)){
$result=$output+$input;
//将结果设置为“out”
}
else if(isset($_POST['sub'])&&!empty($input)){
$result=$output-$input;
//将结果设置为“out”
}
else if(isset($_POST['mul'])&&!empty($input)){
$result=$output*$input;
//将结果设置为“out”
}
else if(isset($_POST['div'])&&!empty($input)){
$result=$output/$input;
//将结果设置为“out”
}
else if(isset($_POST['eq'])和&!empty($input)){
$result=$input;
//将结果设置为“out”
}否则{
$error=“请输入一个数字”;
}
}
<body>
<form action = "calc.php" method = "post">
<input type="text" value="0.0" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>
添加类似于此的内容是的,此功能非常出色!!非常感谢你。我的php中也有一个错误。计算应该是这样的:如果($_POST['add']){$result=$output+$input;}你能用文字解释一下你的解决方案吗
<body>
<form action = "calc.php" method = "post">
<input type="text" value="0.0" name="out" readonly/>
<input type="text" value="0" name="inp"/>
<input type="submit" value="+" name="add"/>
<input type="submit" value="-" name="sub"/>
<input type="submit" value="*" name="mul"/>
<input type="submit" value="/" name="div"/>
<input type="submit" value="=" name="equ"/>
</form>
</body>