HTML表单不是通过PHP发布数据

时间:2015-05-29 10:28:24

标签: php html forms posting

我目前正在创建一个界面供用户输入有关宠物的详细信息。我使用两个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>

2 个答案:

答案 0 :(得分:0)

这将始终退出脚本。

 {
     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代码是否正确并告诉您哪里出错。

答案 1 :(得分:0)

我认为问题在于:

B() : p(new int(10) )  // p is still a junk value here
{
   // p already has a valid value here
}

你在哪里定义 if ($result > 0) ? 尝试检查protected.php中的$result并检查条件是否正确。