![](/img/trans.png)
[英]How can I display 4 TDs in my table then create a new table row using PHP?
[英]how can I display the result table in a new page by using PHP?
尝试将结果显示在新页面中,但是不起作用。 我提交数据后出现错误。总是说定义的功能。 我是编程新手,请帮助我。 谢谢。
<html>
<?php
//define variables and set to empty values
$firstNameErr = $lastNameErr = $ageErr = $areaStudyErr = "";
$firstName = $lastName = $age = $areaStudy ="";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["firstName"]))
{$firstNameErr = "First Name is required";}
else
{
$firstName = test_input($_POST["firstName"]);
// check if section only contains letters and whitesapace
if (!preg_match("/^[a-zA-Z ]*$/",$firstName))
{
$firstNameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["lastName"]))
{$lastNameErr = "Last Name is required";}
else
{
$lastName = test_input($_POST["lastName"]);
// check if section only contains letters and whitesapace
if (!preg_match("/^[a-zA-Z ]*$/",$lastName))
{
$lastNameErr = "Only letters and white space allowed";
}
}
if (empty($_POST["age"]))
{$ageErr = "Age is required";}
else
{
$age = test_input($_POST["age"]);
// check if age only contains numbers
if (!is_numeric($age))
{
$ageErr = "Only numbers allowed";
}
}
if (empty($_POST["areaStudy"]))
{$areaStudyErr = "required section";}
else
{$areaStudy = test_input($_POST["areaStudy"]); }
}
function test_input($data)
{
return $data;
}
?>
<body>
<h2>Student information</h2>
<form method="post" action="submit.php" >
First Name: <input type="text" name="firstName">
<span class="error"> *<?php echo $firstNameErr;?> </span>
<br></br>
Last Name: <input type="text" name="lastName">
<span class="error"> *<?php echo $lastNameErr;?></span>
<br></br>
Age: <input type="text" name="age">
<span class="error"> *<?php echo $ageErr;?></span>
<br></br>
Area of Study: <input type="text" name="areaStudy">
<span class="error"> *<?php echo $areaStudyErr;?></span>
<br></br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
文件Submit.php中的代码,请尝试使用此代码在新页面中显示表格结果
<html>
<body>
<table border="1" cellspacing=0 cellpadding=3>
<tr>
<th>Frist Name</th>
<th>Last Name</th>
<th>Age</th>
<th>Area of Study</th>
</tr>
<tr>
<td><?php echo $firstName; ?></td>
<td><?php echo $lastName; ?></td>
<td><?php echo $age; ?></td>
<td><?php echo $areaStudy; ?></td>
</tr>
</table>
</body>
</html>
在显示它们之前,必须将$ _POST值分配给Submit.php上的变量。
例如
$ firstName = $ _POST ['firstName'];
您在这里犯了一些非常基本的错误,这使我建议您最好将代码放在一边,并先在Code Academy之类的网站上花一些时间: http : //www.codecademy.com/tracks/php
话虽这么说,您的错误是由于尝试使用submit.php
中从未声明过的变量引起的(错误状态)。 这些变量在第一页(包含表单)中声明,并且仅在处理该页时存在于服务器上。
表单提交是全新的HTTP请求 。 由于HTTP是无状态协议 ,因此原始请求期间存在的所有内容将不再存在。 因此,要访问这些变量,您需要复制原始页面上的相同(或相似)逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.