分配给 php 变量的字符串中的 php 问题
我正在为课堂编写第一个 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 函数不会导致输出出现。相反,我得到的是空行。我在字符串赋值中是否错误地定义了它?
I'm writing my first php page for class. The page is supposed to take input from a form and output it. At first I tried to using the $content variable below to store a block of html which would output either the form or the ouput. I was putting this inside the id="content" div depending on whether or not the form elements were set. Anyway, I moved away from this because I wasn't able to use the _SERVER['PHP_SELF'] varialbe correctly. I'm abandoning this for now, but am now running into a similar problem. Here' my code so far:
<?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>
The problem right now is the fact that the echo function inside the $content variable isn't causing the output to appear. Instead I'm getting blank lines. Am I defining it incorrectly inside of the String assignment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
应该是
ECHO 不是函数并且不返回任何内容。您还可以在 PHP 代码中使用
构造。此时
About You
不是 HTML,只是字符串。
Should be
ECHO is not function and return nothing. Also you use
<?php ?>
construction inside PHP code. In this moment<h1>About You</h1>
is not HTML, just string.像这样使用。
Use like this.