User Tools

Site Tools


en:web_development:forms:forms

This is an old revision of the document!


Web Development Lesson 8 - Forms

Forms

Objective

In this activity you will create a form and use it to pass data from one page to another.

Setup

  • We will continue to work with 'form.php' and 'process_form.php'. If you need the code again, here it is.

form.php

<?php
    include('header.php');
    include('menu.php');
?>
    <main>
        <p>Choose your hero.</p>
        <ol>
            <li><a href="process_form.php?name=Loki">Loki</a></li>
            <li><a href="process_form.php?name=Hulk">Hulk</a></li>
        </ol>
    </main>
<?php
    include('footer.php');
?>

**process_form.php**
<code><?php
    include('header.php');
    include('menu.php');
?>
    <main>
        <p>You have chosen <?php echo $_GET['name']; ?></p>
    </main>
<?php
    include('footer.php');
?>

Forms

  • Open 'form.php'. We will display our form here.
  • Add a new line below the opening <main> tag.
  • Type 'form' and select the option that appears.
  • The <form> tag has a parameter called <action>. This is where you want to send your data.
  • Set action to 'process_form.php'.
  • Move the existing paragraph inside the <form> tags.
  • Now add our hero options as 'submit' buttons.
            <button type="submit" name="name" value="Loki"></button>
            <button type="submit" name="name" value="Hulk"></button>
  • Setting the type to 'submit' means that when we click the button it will submit the form and send the data. Both buttons will do this.
  • name is the parameter name to be sent. Submit buttons will often be named 'submit' and not contain data, but sometimes there will be two forms on a page (or sent to a page) and you need to know which one to process.
  • The value is the text that will appear on the button.
en/web_development/forms/forms.1642120185.txt.gz · Last modified: 2023/08/16 09:33 (external edit)