php计算一直得到的答案是0

  • 本文关键字:答案 计算 php php html
  • 更新时间 :
  • 英文 :


这是index.php文件

if(isset($_POST['submit']))
{
echo header('Location:answer.php');
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<form method="post">
<div class="form-group">
<label> Insert P value </label>
<input type="text" name="p" placeholder="please insert your P value">
</div>
<br>
<div class="form-group">
<label> Insert R value </label>
<input type="text" name="r" placeholder ="please insert your R value ">

</div>
<br>
<div class="form-group">
<label> Insert your N value </label>
<input type="text" name="n" placeholder=" please insert your N value">
</div>
<br>
<button type="submit" name="submit" > Submit </button>
</body>
</html>

这是answer.php文件

<?php
$p=isset($_POST['p'])?$_POST['p']: " ";
$r=isset($_POST['r'])?$_POST['r']:" ";
$n=isset($_POST['n'])?$_POST['n']: " ";
$si=(int)$p*(int)$r*(int)$n/100;
echo "Hello this is the final answer ".$si;
?>

我希望答案显示在answer.php的另一个页面上,但无论我插入什么数字,我都会得到0的答案。请帮忙,谢谢你。

您需要将表单直接发布到answer.php。通过将其发布到自己身上,然后重定向,你会丢失所有提交的数据——它会被发送到你的索引页面,然后不会再次传输到答案页面。重定向会导致一个单独的GET请求,并且不包含任何提交的数据。

另一种选择是将所有逻辑移动到index.php中,而根本不需要使用单独的脚本。

根据我从您的代码中看到的内容。在表单标记中,您需要添加action='nswer.php',这是一个指向您请求数据的页面的直接链接,在本例中为answer.php。

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="container">
<form method="post" action="answer.php">
<div class="form-group">
<label> Insert P value </label>
<input type="text" name="p" placeholder="please insert your P value">
</div>
<div class="form-group">
<label> Insert R value </label>
<input type="text" name="r" placeholder ="please insert your R value ">
</div>
<br>
<div class="form-group">
<label> Insert your N value </label>
<input type="text" name="n" placeholder=" please insert your N value">
</div>
<button type="submit" name="submit" > Submit </button>
</body>
</html>

最新更新