0

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?

2 Answers 2

5
$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>';

Should be

$content = '<h1>About You</h1>
    <h4>First Name:</h4>
    <p>'.$fname.'</p>
    <h4>Last Name:</h4>
    <p>'.$lname.'</p>
    <h4>Age:</h4>
    <p>'.$age.'</p>';

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I was thinking of it differently. I thought that once the string rendered as HTML the PHP would become active, but this makes sense. Thank you.
0

Use like this.

$content = '<h1>About You</h1> <h4>First Name:</h4> <p>' . $fname 
    .'</p> <h4>Last Name:</h4> <p>' . $lname
    .'</p> <h4>>Age:</h4> <p>'. $age
    .'</p>';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.