0

我正在尝试在我的 Wamp 服务器上执行一个非常简单的 PHP 发布方法,但是当我单击“提交”时,我返回的是我的 PHP 源代码,而不是它应该做的事情。值得一提的是,我在 Wamp 上做了一些简单的测试,它肯定是在阅读 PHP。

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="" />
<title>308 Assigning Values</title>
<link rel="stylesheet" type="text/css" href="css/basic.css" />

</head>

<body>

<form method="post" action="0308_Assigning_Values.php">

<p>
First Name: 

<input type="text" name="firstname" size="30"/>
</p>

<p>
Last Name: 

<input type="text" name="lastname" size="30"/>
</p>

<p>Your Age: 
</p>
<p><input type = "text" name="age" size="3"/></p>

<p><input type="submit" value="Submit Information" />
</p>


</form>

</body>
</html>

--------------------------------------------------------------

<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

$fullname = "$firstname $lastname";

$factor = 5;

$ageplus = $age + $factor;

$current_year = date('Y');

$birth_year = $current_year - $ageplus;

print "<p>Your name is $fullname ";
print "<p>and you say your age is $age ";
print "<p>but I bet you are really $ageplus ";
print "<p>and were born in $birth_year</p> ";

?>
4

2 回答 2

1

首先,这是作业吗?您的 HTML 文档的标题会使它看起来如此。如果是这样,我不想将解决方案提供给您,因为这对您的学习没有帮助。

不过这里有一个提示:请记住,PHP 只是在 HTML 文档中的位置执行。就像您正在编写一个 HTML 文档一样,然后在您需要计算动态值的地方(您事先不确定)您应该放置一个 PHP 标记来计算并打印出应该存在的 HTML .

请记住,任何有效的 PHP 代码都可以进入此 PHP 标记,因此您可以在其中使用 if/else 语句来确定值是否已发布到服务器。

于 2013-07-08T17:08:49.933 回答
0

尝试这个:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="" />
<title>308 Assigning Values</title>
<link rel="stylesheet" type="text/css" href="css/basic.css" />

</head>

<body>
<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

$fullname = "$firstname $lastname";

$factor = 5;

$ageplus = $age + $factor;

$current_year = date('Y');

$birth_year = $current_year - $ageplus;

print "<p>Your name is $fullname ";
print "<p>and you say your age is $age ";
print "<p>but I bet you are really $ageplus ";
print "<p>and were born in $birth_year</p> ";

?>
<form method="post" action="0308_Assigning_Values.php">

<p>
First Name: 

<input type="text" name="firstname" size="30"/>
</p>

<p>
Last Name: 

<input type="text" name="lastname" size="30"/>
</p>

<p>Your Age: 
</p>
<p><input type = "text" name="age" size="3"/></p>

<p><input type="submit" value="Submit Information" />
</p>


</form>

</body>
</html>

--------------------------------------------------------------

您将输出包括 HTML 在内的整个文档。当您第一次加载页面时它不会打印任何帖子数据的唯一原因是因为帖子数据尚未发送。

希望这可以帮助!

于 2013-07-08T17:11:09.693 回答