【发布时间】:2012-09-12 00:02:20
【问题描述】:
我正在为课堂编写我的第一个 php 页面。该页面应该从表单中获取输入并输出它。起初我尝试使用下面的 $content 变量来存储一个 html 块,该块将输出表单或输出。我将它放在 id="content" div 中,具体取决于是否设置了表单元素。无论如何,我离开了这个,因为我无法正确使用 _SERVER['PHP_SELF'] 变量。我暂时放弃了这个,但现在遇到了类似的问题。到目前为止,这是我的代码:
<?php
if (!empty($_POST['first-name']) && !empty($_POST['last-name'])
&& !empty($_POST['age']))
{
$fname = $_POST['first-name'];
$lname = $_POST['last-name'];
$age = $_POST['age'];
$content = '<h1>About You</h1>
<h4>First Name:</h4>
<p><?php echo $fname; ?></p>
<h4>Last Name:</h4>
<p><?php echo $lname; ?></p>
<h4>Age:</h4>
<p><?php echo $age; ?></p>';
}
else
{
$content = '';
}
//get the first name, last name and age from the form
?>
<html lang="en">
<head>
<title>Personal Info Results</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<div class="content">
<h1>My Form</h1>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div id="person-info">
<label>First Name:</label>
<input type="text" name="first-name"/><br/>
<label>Last Name:</label>
<input type="text" name="last-name"/><br/>
<label>Age:</label>
<input type="text" name="age"/><br/>
</div>
<div id="submit"><input type="submit" value="Submit Info"/></div>
</form>
</div>
<div id="output">
<?php echo $content; ?>
</div>
</body>
</html>
现在的问题是 $content 变量中的 echo 函数没有导致输出出现。相反,我得到了空白行。我是否在字符串赋值中错误地定义了它?
【问题讨论】: