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

Viewed 29

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 h1 tag that says "Enter the name of your favourite musical artist"
  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.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title> Fav Artist</title>
  <meta name="author" content="Sean Kingston">
  <meta name="viewport" content="with=device-width, initial-scale=1.0">
</head>
<body>
<!-- H1 tag I want to change once the user clicks on submit button-->
<h1> Enter the name of your favourite musical artist </h1>
<!--Form-->
<form method="get">
  <input type="text" name="name" />
  <input type="submit" value="Submit" name="sub">
</form>
</body>
</html>
<?php
//isset not working//
 if(isset($_GET['name'])){
       echo $_GET['name'];
   }
?>
    <?php
      show_source(FILE);
      </html>
    ?>
1 Answers

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 :-)

Related