HTML 表单未通过 PHP 发布数据

HTML form is not posting the data through PHP

我目前正在创建一个界面,供用户输入有关其宠物的详细信息。我正在使用两个 php 页面,一个具有 html 表单,允许用户在其中输入数据,另一个用于显示数据。我目前遇到了两个问题,

一个是当点击 HTML 表单上的“提交”按钮时,它不会转到第二页。

第二个是通过文本框输入到第一页的数据没有发布到下一页以便用户获得概览。

我是新手php,我会展示所有的代码,因为我觉得有很多错误。代码不是太繁琐,希望有人能帮助解决这些问题,谢谢。

p.s。第一段代码是用户输入数据的页面,第二段代码是第二页

<?php
echo '<link href="style.css" rel="stylesheet">';
session_start();

$fname = $_POST["fname"];
$petname = $_POST["petname"];
$pettype = $_POST["pettype"]; 
$pdob = $_POST["pdob"];
$appdate = $_POST["appdate"];
$apptime = $_POST["apptime"];
$reason = $_POST["reason"];


 $validated = false;
 $_SESSION['login'] = "";

 if ($result > 0) $validated = true;
 if($validated)
{//Divert to the protected page to display information after.
$_SESSION['login'] = "OK";
 $_SESSION['fname'] = $fname;
 $_SESSION['petname'] = $petname;
 $_SESSION['pettype'] = $pettype;
 $_SESSION['pdob'] = $pdob;
 $_SESSION['appdate'] = $appdate;
 $_SESSION['apptime'] = $apptime;
 $_SESSION['reason'] = $reason;


 header('Location: protected.php');
 }
$_SESSION['login'] = "";
?>
<html>
   
 <body>
 <div align="center">

<h1 class="ribbon">
   <strong class="ribbon-content">Veterinary Appointment Form</strong>
</h1>
 <p><i>Please enter information reguarding your pet</i></p>
     
 <form action="protected.php"
method="post">

 <table>
 <tr>
 <td
align="right">Full Name* : </td>
 <td><input size=\"20\"
type="text" size="20" maxlength="50"
name="fname"></td>
 </tr>
 <tr>
 <td
align="right">Name of Pet* : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="petname"></td>
 </tr>
      <tr>
 <td
align="right">Pet Type* : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="pettype"></td>
 </tr>
      <tr>
 <td
align="right">Pet D.O.B : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="petdob"></td>
 </tr>
      <tr>
 <td
align="right">Date of Appointment* : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="appdate"></td>
 </tr>
           <tr>
 <td
align="right">Time of Appointment* : </td>
 <td><input size=\"20\"
type="text" size="40"
maxlength="50" name="apptime"></td>
 </tr>
                <tr>
 <td
align="right">Why Appoitnment is Needed : </td>
 <td><input size=\"20\"
type="text" size="20"
maxlength="50" name="reason"></td>
 </tr>
 <tr>
 <td> </td>
 <td colspan="2"
align="left"><input type="submit"
value="Login"></td>
</div> </tr>
     <br>

 </table>
 </form>
 </body>
<img src="doglogo.png" alt="Logo" style="width:150;height:130;">
</html>
<?php

echo '<link href="style.css" rel="stylesheet">';
 {
 header('Location: ManageUser.php');
 exit();
 }

?>
<html>
 <head>
 
 <title>Welcome!</title> 

 </head>
<body> 
 <div align="center">
<h1 class="ribbon">
   <strong class="ribbon-content">Veterinary Appointment Form</strong>
</h1>
 <p><i>Here are your details</i></p>

<?php

 echo "<p>Full Name: ";
 echo $_SESSION['fname'];
echo "<br/>";
 echo "<p>Name of Pet: ";
 echo $_SESSION['petname'];
echo "<br/>";
 echo "<p>Pet Type: ";
 echo $_SESSION['pettype'];
echo "<br/>";
 echo "<p>Pet D.O.B: ";
 echo $_SESSION['pdob'];
echo "<br/>";
 echo "<p>Date of Appointment: ";
 echo $_SESSION['appdate'];
echo "<br/>";
 echo "<p>Time of Appointment: ";
 echo $_SESSION['apptime'];
echo "<br/>";
 echo "<p>Why Appoitnment is Needed: ";
 echo $_SESSION['reason'];
echo "<br/>";
 echo "</p>";

 {
 echo "<p><a href='ManageUser.php'><i>Click here to return</i></a></p>";
 }

?>
    
     <a href="#" onclick="window.print();return false;">Print this page</a>
     <br>
     <img src="doglogo.png" alt="Logo" style="width:150;height:130;">
</div>
</body>
    
</html>

这将始终退出脚本。

 {
     header('Location: ManageUser.php');
     exit();
 }

也许您想做这样的事情:

 if (something) // if the user didn't fill the form
 {
     header('Location: ManageUser.php');
     exit();
 }

此外,请注意:

echo '<link href="style.css" rel="stylesheet">';

这将显示 <html> 之前的标签,您可能希望将其放在 <head> 标签内。

您还忘记在第二页添加session_start()

总结:

<?php
 session_start();
 if (something)
 {
 header('Location: ManageUser.php');
 exit();
 }

?>
<html>
  <head>
    <title>Welcome!</title> 
    <link href="style.css" rel="stylesheet">
  </head>

奖金:此代码

<?php

 echo "<p>Full Name: ";
 echo $_SESSION['fname'];
echo "<br/>";
 echo "<p>Name of Pet: ";
 echo $_SESSION['petname'];
echo "<br/>";
 echo "<p>Pet Type: ";
 echo $_SESSION['pettype'];
echo "<br/>";
 echo "<p>Pet D.O.B: ";
 echo $_SESSION['pdob'];
echo "<br/>";
 echo "<p>Date of Appointment: ";
 echo $_SESSION['appdate'];
echo "<br/>";
 echo "<p>Time of Appointment: ";
 echo $_SESSION['apptime'];
echo "<br/>";
 echo "<p>Why Appoitnment is Needed: ";
 echo $_SESSION['reason'];
echo "<br/>";
 echo "</p>";

 {
 echo "<p><a href='ManageUser.php'><i>Click here to return</i></a></p>";
 }

?>

可以替换为:

<p>Full Name: <?=$_SESSION['fname']?></p>
<br/>
<p>Name of Pet: <?=$_SESSION['petname']?></p>
<br/>
<p>Pet Type: <?=$_SESSION['pettype']?></p>
<br/>
<p>Pet D.O.B: <?=$_SESSION['pdob']?></p>
<br/>
<p>Date of Appointment: <?=$_SESSION['appdate']?></p>
<br/>
<p>Time of Appointment: <?=$_SESSION['apptime']?></p>
<br/>
<p>Why Appoitnment is Needed: <?=$_SESSION['reason']?></p>
<br/>
<p><a href='ManageUser.php'><i>Click here to return</i></a></p>

请务必使用 W3C Validation tool 检查呈现的代码。这将检查 HTML 代码是否正确并告诉您哪里出了问题。

我认为问题出在这里:

 if ($result > 0)

你在哪里定义了$result? 尝试在 protected.php 中检查 print_r($_POST); 并检查您是否符合条件。