Php Echo不显示我的结果/答案
我正在使用PHP7.0、apache2、mysql版本14.14发行版5.6.17和phpmyadmin 5.7 由于某种原因,我的php文件不会显示变量$result的值。每当我尝试使用“echo”时,它都不会显示它。甚至没有另一个变量(总和) 我的html文件包含以下代码:Php Echo不显示我的结果/答案,php,lamp,php-7,Php,Lamp,Php 7,我正在使用PHP7.0、apache2、mysql版本14.14发行版5.6.17和phpmyadmin 5.7 由于某种原因,我的php文件不会显示变量$result的值。每当我尝试使用“echo”时,它都不会显示它。甚至没有另一个变量(总和) 我的html文件包含以下代码: <!doctype html> <html lang="en-us"> <head> <title>calculation form</title> <m
<!doctype html>
<html lang="en-us">
<head>
<title>calculation form</title>
<meta charset="utf-8">
</head>
<body>
<form method="post" action="calculate.php">
<p>Value 1: <input type="text" name="val1" size="10"></p>
<p>Value 2: <input type="text" name="val2" size="10"></p>
<p>Calculation:<br>
<input type="radio" name="calc" value="add"> add<br>
<input type="radio" name="calc" value="subtract"> subtract<br>
<input type="radio" name="calc" value="multiply"> multiply<br>
<input type="radio" name="calc" value="divide"> divide
</p>
<p><input type="submit" name="submit" value="Calculate"></p>
</form>
</body>
</html>
计算表
值1:
价值2:
计算:
添加
减去
乘
分
我的php代码包含以下内容:
<?php
$sum = 0;
if(($_POST[val1] == "") || ($_POST[val2] == "") || ($_POST[calc] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST[calc] == "add")
{
$result = $_POST[val1] + $_POST[val2];
}
if($_POST[calc] == "subtract")
{
$result = $_POST[val1] - $_POST[val2];
}
if($_POST[calc] == "multiply")
{
$result = $_POST[val1] - $_POST[val2];
}
if ($_POST[calc] == "divide") {
$result = $_POST[val1] / $_POST[val2];
}
?>
<?php
echo $result;
echo $sum;
?>
您必须在if条件内进行回显,或者必须全局设置变量
<?php
$sum = 0;
if(($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST['calc'] == "add")
{
$result = $_POST['val1'] + $_POST['val2'];
echo $result;
}
if($_POST['calc'] == "subtract")
{
$result = $_POST['val1'] - $_POST['val2'];
echo $result;
}
if($_POST['calc'] == "multiply")
{
$result = $_POST['val1'] - $_POST['val2'];
echo $result;
}
if ($_POST['calc'] == "divide") {
$result = $_POST['val1'] / $_POST['val2'];
echo $result;
}
?>
您在$\u POST[val1]
它应该是$\u POST['val1']
用示例更新
<?php
$sum = 0;
$_POST['val1']=5;
$_POST['val2']=10;
$_POST['calc']='add';
if(($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST['calc'] == "add")
{
$sum = $_POST['val1'] + $_POST['val2'];
}
if($_POST['calc'] == "subtract")
{
$sum = $_POST['val1'] - $_POST['val2'];
}
if($_POST['calc'] == "multiply")
{
$sum = $_POST['val1'] - $_POST['val2'];
}
if ($_POST['calc'] == "divide") {
$sum = $_POST['val1'] / $_POST['val2'];
}
echo $sum;
?>
我试过了,但没用。它不会显示任何内容。请参见$_POST[val1],它应该在单引号中引用$_POST['val1']Ok,因为某些原因,当我将echo添加到所有有效的条件语句中时。非常感谢,兄弟。我还尝试在开始时初始化变量。Smdh为什么我之前不明白…太尴尬了。再次非常感谢:)我会的。在我接受答案之前,我有5分钟的时间@约瑟芬,好的
<?php
$sum = 0;
$_POST['val1']=5;
$_POST['val2']=10;
$_POST['calc']='add';
if(($_POST['val1'] == "") || ($_POST['val2'] == "") || ($_POST['calc'] == ""))
{
header("Location: calculate_form.html");
exit;
}
if($_POST['calc'] == "add")
{
$sum = $_POST['val1'] + $_POST['val2'];
}
if($_POST['calc'] == "subtract")
{
$sum = $_POST['val1'] - $_POST['val2'];
}
if($_POST['calc'] == "multiply")
{
$sum = $_POST['val1'] - $_POST['val2'];
}
if ($_POST['calc'] == "divide") {
$sum = $_POST['val1'] / $_POST['val2'];
}
echo $sum;
?>