Forms

The same can be used with form data. You might have previously created forms in HTML but without some sort of server side language, there is a very limited number of things you can do with that data. Now when we submit a form we can use the variables submitted in our page.

The form data is in a Struct called Form. We can access the individual pieces typing form.variablename.

<form action="#cgi.script_name#?p=#p#" method="post">
    <div class="mb-3">
        <label for="firstName" class="form-label">First Name</label>
        <input type="text" class="form-control" id="firstName" name="first" placeholder="First Name">
    </div>
    <div class="mb-3">
        <label for="lastname" class="form-label">Last Name</label>
        <input type="text" class="form-control" id="lastname" name="last" placeholder="Last Name">
    </div>
    <div class="mb-3">
        <label for="exampleFormControlInput1" class="form-label">Email address</label>
        <input type="email" class="form-control" id="exampleFormControlInput1" name="email" placeholder="name@example.com">
    </div>
    <div>
        <button class="btn btn-primary">Submit</button>
    </div>
</form>

A form has several different tags in it. Here are the basics:

<form action="#cgi.SCRIPT_NAME#?p=#p#" method="post"> - The <form> tag has two essential properties. The first is the action property. This is the file to which the browser will pass the form variables. Put another way, it's the page that will load when you submit the form. In this case, we are using BoxLang variables that will automatically put the name of the same page. If you use this, don't forget to wrap it in <bx:output>.

The "method" property says how the variables will be submitted. The two most common methods are <POST> and <GET>. For the moment, let's keep it simple. Using "get" will put the variables and their values in the address bar or in the URL scope. Using "post" will submit them another way which is behind the scenes, not in the URL. <input type="text" name="firstname" placeholder="First Name" value="" /> - The input tag defines a place to put in a variable. You name the variable by using the "name" property in the <input> tag. The value is whatever is entered by the user or you predefine with the value property.

<button class="btn btn-primary">Submit</button> can all so written like this: <input type="submit" value="save" /> - Both of these methods create a button that will submit the form. To make a button on your form that does NOT submit it, add a type property like this <button type="button" ...>Save</button>

For more HTML information about forms visit: http://www.w3schools.com/html/html_forms.asp

Question

In the MyForm.bxm page in the leanBoxLang folder, use HTML to create a form which submits to the same page. You can use the one above as an example. Make your form with 5 inputs that coincide with some of the titles in your 5 Books assignment. On the page include <bx:dump var="#form#" label="The Form Data" /> so you can test the submission of the form.

Lesson 8 of 12 - Learn BoxLang