[SOLVED] I’m trying to figure out how to get the variable $_GET to work so that the H1 tag turns into what the user imputted

Issue

This Content is from Stack Overflow. Question asked by yermteam

The question I’m trying to answer is below

1.Create a new PHP file named lab2.php.
2. Inside, add the HTML skeleton code and give its title “Lab Week 2″
3. Within the body tag, create a h2 tag with your full name
4. Add a section tag inside the body tag.
5.Create a form tag and use the $_GET method to get users to input their favorite artist. Store the user input into variables.
6.Create a submit button for the user to input their favorite artist.
7.Check whether the variable is empty or not.

HTML CODE

PHP CODE



Solution

i’m gonna hazard a guess and asume that this is some sort of educational task.

but nevertheless here is how i understand the tasks.

<?php
//1.Create a new PHP file named lab2.php.
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Lab Week 2</title><!-- 2.Inside, add the HTML skeleton code and give its title “Lab Week 2"  -->
</head>
<body>
    <h1><?php
        // 5. use the $_GET method to get users to input their favorite artist. Store the user input into variables.
        $favouriteMusicalArtist = $_GET['favourite_musical_artist'];

        // 7.Check whether the variable is empty or not.
        if (empty($favouriteMusicalArtist)) {
            //3.Within the body tag, create a h1 tag that says "Enter the name of your favourite musical artist"
            echo 'Enter the name of your favourite musical artist';
        } else {
            echo 'Your favourite musical artist is: ' . $favouriteMusicalArtist;
        }
    ?></h1>
    <!-- 4.Add a section tag inside the body tag. -->
    <section>
        <!-- 5.Create a form tag -->
        <form method="GET">
            <input type="text" name="favourite_musical_artist"/>
            <!-- 6.Create a submit button for the user to input their favorite artist.  -->
            <button type="submit">Submit</button>
        </form>
    </section>
</body>
</html>

Now if this is some sort of test don’t just copy of the internet. Do some fiddling around with the code and try to learn something 🙂


This Question was asked in StackOverflow by yermteam and Answered by Sicet7 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?