User Tools

Site Tools


en:web_development:forms:forms

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:web_development:forms:forms [2022/01/13 16:29]
mag created
en:web_development:forms:forms [2023/08/16 09:33] (current)
Line 1: Line 1:
 ====== Web Development Lesson 8 - Forms ====== ====== Web Development Lesson 8 - Forms ======
-===== Forms =====+===== Inputs =====
 ==== Objective ==== ==== Objective ====
 In this activity you will create a form and use it to pass data from one page to another. In this activity you will create a form and use it to pass data from one page to another.
Line 21: Line 21:
 <?php <?php
     include('footer.php');     include('footer.php');
-?>+?></code>
  
 **process_form.php** **process_form.php**
Line 35: Line 35:
 ?></code> ?></code>
  
-==== Forms ====+==== Submit a Form ====
   * Open 'form.php'. We will display our form here.   * Open 'form.php'. We will display our form here.
   * Add a new line below the opening <html><main></html> tag.   * Add a new line below the opening <html><main></html> tag.
   * Type 'form' and select the option that appears.   * Type 'form' and select the option that appears.
-  * The <html><form></html> tag has a parameter called <html><action></html>. This is where you want to send your data.+  * The <html><form></html> tag has a parameter called <html>action</html>. This is where you want to send your data.
   * Set <html>action</html> to 'process_form.php'.   * Set <html>action</html> to 'process_form.php'.
 +<code>        <form action="process_form.php"></code>
   * Move the existing paragraph inside the <html><form></html> tags.   * Move the existing paragraph inside the <html><form></html> tags.
   * Now add our hero options as 'submit' buttons.   * Now add our hero options as 'submit' buttons.
Line 46: Line 47:
             <button type="submit" name="name" value="Hulk"></button></code>             <button type="submit" name="name" value="Hulk"></button></code>
   * Setting the <html>type</html> to 'submit' means that when we click the button it will submit the form and send the data. Both buttons will do this.   * Setting the <html>type</html> to 'submit' means that when we click the button it will submit the form and send the data. Both buttons will do this.
-  * <html>name</html> 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.+  * <html>name</html> 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. We use that here to differentiate the selected heroes.
   * The <html>value</html> is the text that will appear on the button.   * The <html>value</html> is the text that will appear on the button.
-  * +  * Save and upload the file, then open the page. 
 +  * Click on your chosen hero then check the results. Do you see your hero's name? 
 +  * This is an usual way to use a Submit button, but it's valid. Let's look at other options. 
 + 
 +==== Text Input ==== 
 +  * Let's create a form to add heroes to our database. 
 +  * We'll need input fields for the name, whether they're a hero or a villain, the year they first appeared in Marvel comics (or elsewhere) and what the source of their power is. 
 +  * We could use a table to present our form with labels on the left and inputs on the right, but that isn't responsive, that is, it won't adapt to smaller screens. 
 +  * Instead, we'll use a list, where each label / input pair is a list item. 
 +  * Delete everything inside the <html><form></html> tag and add the following code. 
 +<code>                <ul> 
 +                    <li> 
 +                    </li> 
 +                </ul></code> 
 +  * The first field for the hero's name can be a simple text field. 
 +<code>                    <li> 
 +                        <label>Hero Name:</label> 
 +                        <input type="text" name="name" size="30" maxlength="50" hint="Iron Man"> 
 +                    </li></code> 
 +  * Here we have a label for our input, using the <html><label></html> tag. 
 +  * We then have an input, but rather than being of <html>type="button"</html> or <html>type="submit"</html>, it is <html>type="text"</html> which is used for a simple text field, usually up to 100 characters or so. 
 +  * We give it a <html>name</html> that will inform 'process_form.php' what the value refers to. 
 +  * There are two additional attributes that we haven't seen before. <html>size</html> sets the size of the input box to 30 characters and <html>maxlength</html> limits the length of characters that can be entered. 
 +  * Finally, we have the option to give the user a hint as to what kind of content we expect here using <html>hint</html>
 +  * Save and upload the code to see how it looks live. 
 + 
 +==== Radio Buttons ==== 
 +  * When we have an input with only a few set options and only one can be selected at a time, we use radio buttons. 
 +  * The most common example of this is 'male' and 'female'
 +  * In this case we want to know whether our hero is actually heroic or villainous. Add the following code to show this. 
 +<code>                    <li> 
 +                        <label>Side:</label> 
 +                        <input type="radio" name="side" value="Hero" checked="checked"> Hero<br> 
 +                        <input type="radio" name="side" value="Villain"> Villain 
 +                    </li></code> 
 +  * I've labelled this hero/villain distinction 'side' but if you can think of a better term, feel free to use that. 
 +  * Again, radio buttons use the <html><input></html> tag and, like buttons, they have a name and a value. 
 +  * The <html>value</html> isn't displayed as it is on a button, because there's no room on a radio button. Instead, the value is the text we want to send to 'process_form.php'
 +  * We add the text before or after the <html><input</html> depending on how we want the form to appear. 
 +  * Finally, notice the <html>checked</html> attribute in the first input. This sets the default item if you want one. 
 +  * Save and upload to see how it looks. 
 + 
 +==== Number ==== 
 +  * Our next field is the year that the hero first appeared in a comic (or movie etc). 
 +  * It will be sent as text, but it's actually a number and we can help our user by specifying that. 
 +  * Add the following code to your form. 
 +<code>                    <li> 
 +                        <label>First Appearance:</label> 
 +                        <input type="number" name="year"> 
 +                    </li></code> 
 +  * We can also set <html>size</html> and <html>maxlength</html>, but we'll look at better ways to manage this later. 
 +  * Save and upload the code to see how it looks. Almost exactly the same a text input, right? 
 +  * Notice that it has up and down arrows to allow users to change the number by increments. 
 +  * If you have a mobile to load the page with, you'll also notice that the keyboard automatically changes to a numeric input rather than the usual keyboard layout. 
 + 
 +==== Dropdown Boxes ==== 
 +  * Our next field is the source of the hero's power. This is a type of category, where we want to control the options that can be selected. 
 +  * For this we use a <html><select</html> field. 
 +  * Copy the following code into your form. 
 +<code>                    <li> 
 +                        <label>Source of Power:</label> 
 +                        <select name="power"> 
 +                            <option value="skill">Skill</option> 
 +                            <option value="bio">Biological</option> 
 +                            <option value="magic">Magic</option> 
 +                            <option value="tech">Technology</option> 
 +                            <option value="mutant">Mutant</option> 
 +                        </select> 
 +                    </li></code> 
 +  * We have a number of options listed using <html><option></hmtl> tags and they all sit inside the <html><select></html> tag. 
 +  * Rather than setting the <html>name</html> for each option individually, we set it once in the <html><select></html> tag. 
 +  * The value sent to 'process_form.php' is taken from the <html>value</html> attribute, while the user sees the text between the <html><option></html> tags. 
 +  * Save and upload the file to see that it appears as a dropdown box. 
 +  * Finally, add the submit button to make the form work. 
 +<code>                <input type="submit" name="submit" value="Add Hero"></code> 
 +  * Save and upload the file. 
 +  * Add some data and click on the button to submit your data. 
 +  * Notice that if you choose 'Biological' as the source of power, the result is sent as 'power=bio' rather than 'power=Biological' because of the <html>value</html> associated with that option. 
 + 
 +==== Fieldsets ==== 
 +  * We'd also like to be able to add the appearances
en/web_development/forms/forms.1642120185.txt.gz · Last modified: 2023/08/16 09:33 (external edit)