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?