This week we wrapped up our discussion about forms by looking at some other frequently used modes of form input, such as drop-downs, radio buttons and checkboxes.
Here is the finalised, and working, example code:
###index.html
###script.js
###backend/savedata.php
We also talked about troubleshooting some common JavaScript mishaps, and using Chrome Dev Tools (F12) to help find and resolve them. These common errors include:
mismatched id references, between HTML id attribute and JavaScript getElementById
misspelt JavaScript function calls e.g. XMLHttpRequest not XMLHTTPRequest and innerHTML not innerHtml
wrong CSS stylesheet file name / JavaScript file name
In all these cases, keep in mind case-sensitivity.
A couple of other points relevant to getting the final version to work:
Make sure the line that does the INSERT in your PHP file is not commented out! I doubt that would be the case in anyone else’s code, and I still have no idea why it was commented out in mine, but it was. As a result, nothing was being saved to the database, obviously.
No spaces in HTML attributes, e.g. id="myform" not id = "myform"
Backticks vs. quotes. Make sure the field names in the INSERT statement (in the PHP file) are enclosed in backticks, not in quotes. It’s an SQL thing.