【发布时间】:2015-05-29 10:28:24
【问题描述】:
我目前正在创建一个界面,供用户输入有关其宠物的详细信息。我正在使用两个 php 页面,一个带有允许用户输入数据的 html 表单,另一个用于显示数据。我目前遇到了两个问题,
一个是当点击HTML表单上的“提交”按钮时,它不会跳转到第二页。
第二个是通过文本框输入到第一页的数据没有发布到下一页供用户概览。
我对php很陌生,我会显示所有的代码,因为我觉得有很多错误。代码不是太繁琐,希望有人能帮助解决这些问题,谢谢。
附言第一段代码是用户可以输入数据的页面,第二段是第二个页面。
<?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>
【问题讨论】:
-
你的表格在哪里?
-
exit()将停止 PHP 在该点之后执行 -
在标题之前回显styles.css链接标签会破坏事情,这意味着表单可能不起作用 - 链接标签应该包含在HTML头中,而不是在重定向之前输出。在第一个脚本中,$result 没有要测试的价值。