How to style PHP output?

Does anyone know how to output this in PHP? Have much of the code already. I'm just entering the PHP realm.?

  • Take this HTML form: <html> <body> <form action="age_form_submit.php" method="POST"> <div><label for="first_name">First Name:</label> <input type="text" name="first_name" size=30></div> <div><label for="last_name">Last Name:</label> <input type="text" name="last_name" size=30></div> <div><label for="age">Age:</label> <input type="text" name="age" size=2></div> <div><label for="submit"> </label> <input type="submit" value="Send It!"></div> </form> </body> </html> And save it as age_form.php on your server. Now, create a new document: ========================== <?php $adult=array('cigars', 'galavanting', 'voting', 'stuff from when you were a kid', 'old things'); $child=array('bubble gum', 'Teletubbies', 'thumb sucking'); // You'll probably want to do some stuff, here. ?> <html> <body> <!-- You'll want to do some more PHP, here --> </body> </html> ========================== And save it as age_form_submit.php on your server. Here is your goal: When a user completes and submits the form, age_form_submit.php should output: Hello [the submitted first name] [the submitted last name]! You like: [output this portion if the age is 18 or over] 1. cigars 2. galavanting 3. voting 4. stuff from when you were a kid 5. old things [output this portion if the age is less than 18] 1. bubble gum 2. Teletubbies 3. thumb sucking Use the appropriate HTML tags when outputting this data. Use foreach on some array (depending on the age of the user) to output the appropriate unordered list. All three form fields are required. If the user fails to fill out any of them, send her/him back to the form, output an error message, and make sure that the previously submitted values are visible in each form field

  • Answer:

    The answerer above simply outputted the age. Assuming you're allowed to edit the HTML form to add some PHP, here is your completed 'work': age_form_submit.php: <?php session_start(); $adult = array('cigars', 'galavanting', 'voting', 'stuff from when you were a kid', 'old things'); $child = array('bubble gum', 'Teletubbies', 'thumb sucking'); if (isset($_POST["first_name"]) && isset($_POST["last_name"]) && isset($_POST["age"]) && !empty($_POST["first_name"]) && !empty($_POST["last_name"]) && !empty($_POST["age"])) { extract($_POST); } else { $_SESSION["error"] = "You need to fill out all three fields."; header("Location: age_form.php"); } ?> <html> <body> <?php echo "Hello " . $first_name . " " . $last_name . "! \n"; echo "You like: \n"; echo "<ul>\n"; if ($age < 18) { foreach($child as $action) { echo "<li>" . $action . "</li>"; } } else { foreach($adult as $action) { echo "<li>" . $action . "</li>"; } } ?> </body> </html> age_form.php: <?php session_start(); ?> <html> <body> <form action="age_form_submit.php" method="POST"> <div><label for="first_name">First Name:</label> <input type="text" name="first_name" size="30"></div> <div><label for="last_name">Last Name:</label> <input type="text" name="last_name" size="30"></div> <div><label for="age">Age:</label> <input type="text" name="age" size="2"></div> <div><label for="submit"> </label> <input type="submit" value="Send It!"></div> </form> <?php if (isset($_SESSION["error"])) { echo $_SESSION["error"]; unset($_SESSION["error"]); unset($_SESSION); session_destroy(); } ?> </body> </html>

Clugswor... at Yahoo! Answers Visit the source

Was this solution helpful to you?

Other answers

you have to post the input from the form to variables within age_form_submit.php . <?php // start [ age_form_submit.php ] $firstname = $_POST[first_name]; ?> then use the variable in html layout <p><?php $firstname ?></p>

Mark O

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.