I have a confession…. I started reading up and playing around with PHP during semester break, coz you know, I didn’t have anything better to do at work (just kidding). But what I manage to do was build the PHP component for a booking enquiry form that captures the data inputs from the customer, validate it and send it off to do something else.
Here’s a link – dmtsar.0fees.net/process.php, don’t expect too much though. It’s still a bit rough and not very exciting but the basic functionality is there. What I would just need to work on further is the tidy up, formatting ( I was still using tables for layout, OMG – see previous blog )and adding the functions to send Emails and SMS’s. (Test it out, and try leaving out the “mandatory fields” and see what it does). There’s no database connection there yet either.
I want to share are some of my lessons learnt. Firstly, what was I actually trying to achieve here?
1. Ask users some questions – form
2. Capture their answers – submit form
3. Check their answers and make sure they answered correctly and didn’t leave anything out – validate form
4. Tell them what questions were not answered correctly and what information they left out, and give them another chance to answer it – feedback
5. Repeat 1-4 until all questions adequeatly answered.
6. Proceed to the next step – eg, send an email (php, but it would really depend what the next step is)
That sounds pretty obvious. However, I think it’s good programming practise to break down the solution into smaller steps like so. If nothing else, it would help you identify early on what are the common and repeatable functions. I didn’t do this by the way. I just went in there and started hacking away and as a result, I had to completely rewrite my code at least three times!!
Lesson learnt No 1. Make some effort and time to break down the problem at hand and step through your proposed solution. Back in the old days, this use to be called “Pseudocode” [1], but with a little bit less structure and algorithm like. The concept is pretty much the same though.
In my example, if I had actually thought about what I was doing the first time round, I would have realised that some of my code would need to be “repeatable” in certain situations. Put in other words, you know when you’re filling in a form on the web to sign up for something and it’s a long and laborious form and finally you get to click on the OK or SUBMIT button but then it brings you back and its says “Whoopsy, you forgot something”, but it makes you fill everything in ALL OVER AGAIN. That annoys me. Don’t do that.
Lesson learnt No 2. Validation – don’t make the users fill things out more than once.
To solve this dilemma I needed some functions.
Lesson learnt No 3. Functions.
“A function is a block of code that can be executed whenever we need it” [2]. You can also think of functions as a block of code that is common, or that is repeatable, in different parts of your code. Going back to my example, I could create steps 1-4 as functions and my main code would call the function appropriate depending on the situation, or event, that has occurred. In pseudocode it would look something like:
if submit button has been pressed
go to validate_form function
otherwise
go to new_form function
In validate_form function, you could do all sorts of fun, like checking if a “phone number” fields contains “letters”. Etc, anyways, I think you can see the picture here.
Congratulations, you’ve made it to the end !! That was a long rant.
Reference:
[1] http://en.wikipedia.org/wiki/Pseudocode
[2] http://w3schools.com/php/php_functions.asp