INFO 2480-61: Website Database Implementation
  • Syllabus
  • Welcome Letter
  • Week 1: Software and Setup
    • Folder Structure
    • Git: Getting Started
      • Installing a Git Client
      • Creating a Local Repository
      • Making Changes and Merging them
      • Git Wrap Up
    • Git: Github
      • Creating a New Account
      • Creating a Repo on Github
      • Cloning and Forking
      • Cloning a Repo from Github
      • Pushing Changes to Github.com
      • Github: Adding a Collaborator
    • Command Box
      • Downloading the Software
      • Unzipping and Running the Software for the first time
      • Using CommandBox And Installing the UML-Info package.
      • Setting Up Your Local Server
      • Last Step
    • IDE ( Integrated Development Environment )
      • VS Code
      • IntelliJ
    • Journal
  • Week 2: Review of HTML and CSS
    • Making Point In Time and Working Branches
    • Opening our Project from Week 1 and Starting our BoxLang server
    • HTTP Calls and Requests
    • Making a Static Website Dynamic
    • Creating The Management Index.bxm
    • Creating An HTML Form with Bootstrap
    • Last Steps
  • Week 3: Working Forms and our First Table
    • Data Types
    • Case Types and Naming Conventions
    • Database Tools and Your First DB
      • Installing MySql WorkBench
      • Connecting to your MySQL database
      • Creating Your First Table
    • Working Forms
      • Separation of Concerns
      • Configuring Our Site To Use Our Database
      • Capturing Your Form Data
      • Creating Our Server Side Logic
  • Week 5: SQL and Modelling
    • Our Project And Its Users
    • Introduction to SQL
    • Completing our Articles page.
      • Adapting manageArticles.bxm to display existing articles in the database
      • Completing ManageArticles.bxm to Edit Existing Articles.
      • Making Active Articles Appear On The Public Page
  • Week 6: Managing Books
    • Introduction To Modelling
    • Our Data Models
    • Adapting Our Management Page To Be Multi-Tool
    • Creating The Manage Books Page
    • Adding Search To Our Front Index Page
  • Week 7: Working with Selects, Files, WYSIWYG and more.
    • Adding WYSIWYG Capabilities
    • Adding WYSIWYG to the Manage Books Tool
    • Adding Images To Our Store
    • Creating Our Publisher Select Control
  • Week 9: Joins and Better Searching
    • Displaying Our Publisher
    • Searching By Publisher
    • Creating a Browse by Genre
      • Adapting Our Database
      • Building our Queries: Part 1
      • Assigning Genres to a Book in our AddEdit.bxm page
      • Building our Queries: Part 2
      • Building the GenreNav.bxm
      • Adapting The Details.bxm Page to Search By Genre
  • End of Project Checkllist
Powered by GitBook
On this page
  • Telling our manageArticles.bxm file to only send data when it receives it from the form.
  • Create a CLASS file which saves data to our DB
  • Tell our manageArticles.bxm file where to send it the data it receives
  • Check the Database
  1. Week 3: Working Forms and our First Table
  2. Working Forms

Creating Our Server Side Logic

Goal: By the end of this document, you will have created a .bx file to handle saving data from our first form and into your database.

PreviousCapturing Your Form DataNextWeek 5: SQL and Modelling

Last updated 3 months ago

Now that we have a working form, we can use the form data and save it to our database. To do that we need to do four things

  1. Tell our manageArticles.bxm file to only send data when it receives it from the submitted form.

  2. Create a CLASS file which saves data to our DB

  3. Tell our manageArticles.bxm file where to send it the data it receives

  4. Check the database to see if it saved.

Telling our manageArticles.bxm file to only send data when it receives it from the form.

You might have noticed that the green dump appears on your manageArticles.bxm page even where there is no data submitted. This is because all data submitted in a form is collected and saved in a special variable called "FORM". That variable exists whether a form has been submitted or not.

To tell if our form as been submitted or not, we can add an IF statment and put some instructions inside of that. These instruction will run only IF the conditions in the statement are fulfilled.

  1. Edit the top of your manageArticles.bxm page to this

    <bx:if form.keyExists("title")>
        <bx:dump var="#form#" />
    </bx:if>
  2. Load your page. You should NOT see the green dump out box.

  3. Enter some data into your form and submit it. Now you should see the contents of your form displayed in the green dump out box like before.

Create a CLASS file which saves data to our DB

Since we want to follow good separation of concerns, we are going to put all of our "controller" or "logic" in its own folder structure. Inside the bookstore folder, create a folder named "common".

  1. In the common folder, create a file called articles.bx. All of the logic which has to do with articles, we are going to put into this folder.

  2. At the top of the file, put in: class { }

  3. Now we are going to create a function inside that class. A function is just a collection of code that has a name and can be called whenever it is needed. We'll go more into functions next week but for now, just follow the code here. We're going to name our function saveArticle.

    function saveArticle( ){
       
    }
  4. Our function is going to need several pieces of data to do it's job. In the parentheses after the function name, we are going to list the pieces of data it is expecting, what type of data it should be and whether or not it is required.

    function saveArticle( required string title="", required string description="", numeric status=0 ){
    
    }

  5. And now we need to actually put in the part of the code which will do the job we want. In this case, save our data to the database.

        function saveArticle( required string title="", required string description="", numeric status=0 ){
            queryExecute("
                INSERT IGNORE into articles (title, description, status) 
                VALUES (:title, :description, :status)
            ",{
                title:arguments.title,
                description:arguments.description,
                status: arguments.status
            });
        }

Tell our manageArticles.bxm file where to send it the data it receives

The last step is to tell our form to send our submitted data to our articles.bx file.

  1. In manageArticles.bxm, edit the if statement at the top of the page to this:

We are going to go over in much more detail what all of this code means next week but for now, let's see if we can get this working.

  1. Save the file.

  2. Open your manageArticles.bxm page and submit the form.

  3. Don't worry if you have errors. They are normal and part of the process. Compare your code with the code here and see where it is different.

Check the Database

  1. Right click on arrticles and choose select rows limit 1000. An output of the data in your articles table should appear in the main part of the screen. Did it?

  2. Let me know if you have any questions.

Open MySQL Workbench using the same direction from .

From the left hand side under Schemas and your Username, expand Tables. You should see articles. If not, follow the directions .

before
here