Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts
Friday, May 18, 2007
Wednesday, May 16, 2007
Squash That Bug!
Sometimes programming goes awry for the slightest of errors. In our lingo, we call those nasty things bugs.
I just spent maybe half an hour debugging a code that went wrong just because I forgot the "$" in front of a couple of my variable names.
Aargh. That's programming for you...
Anyways, I've learnt quite a bit of stuff on PHP by now. One of which is file operations such as reading & writing to the file.
Here, I'm gonna be awful nice & share...
Reading from a file:
$c_file = "counter.txt";
$check_counter = fopen($c_file, "r");
$curr_counter = fread($check_counter, filesize($c_file));
fclose($check_counter);
The code reads a counter value stored in the file counter.txt. $check_counter is a file handler which opens the file for reading (specified by the parameter "r"). $curr_counter is the variable which is assigned the value of the counter read from the file. The last line quite obviously closes the file after reading.
Writing to a file:
//read counter value from file counter.txt
$counter_file = "counter.txt";
$get_counter = fopen($counter_file, "r");
$counter = fread($get_counter, filesize($counter_file));
//close the counter.txt file
fclose($get_counter);
//open counter.txt file for writing
$alter_counter = fopen($counter_file, "w");
//decrement counter value by 1
$new_counter = $counter-1;
//write new decremented counter value to counter.txt then close the file
fwrite($alter_counter, $new_counter);
fclose($alter_counter);
This code writes a new counter value to the file counter.txt in 2 steps. First the value is read from the file then the file closed, then in the 2nd part minus 1 from $counter & store the new value in the file counter.txt.
I'm sure there are more efficient ways of doing this. If any veteran out there sees this & doesn't mind sparing me a lesson on how, please drop a comment.
& in the meantime, I spent more than half my day trying to figure out how to either call javascript functions from within PHP script or using javascript code to read my counter value from the file. All just to find a way of disabling the form elements when the registration limit is reached (determined by the counter value of zero). Only to talk to my boss later in the evening & realise that he doesn't mind me just echoing a statement in the window to the effect that the user is informed that their registration is rejected because the registration limit has been reached.
Hmmph.
Oh well, at least I am learning new things everyday. I did learn that there is such a thing as server side Javascript. & I did learn those small bits about event handling for stuff that should happen when HTML code loads onto a page. It's done like this:
< onload="check_limit()">
check_limit() being the javascript function, of course. The comment tags are just so that this post gets by the blogger specifications since HTML tags aren't technically allowed on the post :P
Okies, enough rambling. I aim to be home soon.
I just spent maybe half an hour debugging a code that went wrong just because I forgot the "$" in front of a couple of my variable names.
Aargh. That's programming for you...
Anyways, I've learnt quite a bit of stuff on PHP by now. One of which is file operations such as reading & writing to the file.
Here, I'm gonna be awful nice & share...
Reading from a file:
$c_file = "counter.txt";
$check_counter = fopen($c_file, "r");
$curr_counter = fread($check_counter, filesize($c_file));
fclose($check_counter);
The code reads a counter value stored in the file counter.txt. $check_counter is a file handler which opens the file for reading (specified by the parameter "r"). $curr_counter is the variable which is assigned the value of the counter read from the file. The last line quite obviously closes the file after reading.
Writing to a file:
//read counter value from file counter.txt
$counter_file = "counter.txt";
$get_counter = fopen($counter_file, "r");
$counter = fread($get_counter, filesize($counter_file));
//close the counter.txt file
fclose($get_counter);
//open counter.txt file for writing
$alter_counter = fopen($counter_file, "w");
//decrement counter value by 1
$new_counter = $counter-1;
//write new decremented counter value to counter.txt then close the file
fwrite($alter_counter, $new_counter);
fclose($alter_counter);
This code writes a new counter value to the file counter.txt in 2 steps. First the value is read from the file then the file closed, then in the 2nd part minus 1 from $counter & store the new value in the file counter.txt.
I'm sure there are more efficient ways of doing this. If any veteran out there sees this & doesn't mind sparing me a lesson on how, please drop a comment.
& in the meantime, I spent more than half my day trying to figure out how to either call javascript functions from within PHP script or using javascript code to read my counter value from the file. All just to find a way of disabling the form elements when the registration limit is reached (determined by the counter value of zero). Only to talk to my boss later in the evening & realise that he doesn't mind me just echoing a statement in the window to the effect that the user is informed that their registration is rejected because the registration limit has been reached.
Hmmph.
Oh well, at least I am learning new things everyday. I did learn that there is such a thing as server side Javascript. & I did learn those small bits about event handling for stuff that should happen when HTML code loads onto a page. It's done like this:
< onload="check_limit()">
check_limit() being the javascript function, of course. The comment tags are just so that this post gets by the blogger specifications since HTML tags aren't technically allowed on the post :P
Okies, enough rambling. I aim to be home soon.
Event Handling & Forms Forms Forms
The long & short of it is that I'm needing to re-examine Javascript again. Something about it scares me, even though from previous experiences I've seen that it's not anywhere near being an inconquerable giant like say, Java, which I haven't ever touched at all before.
Yet, that is.
In any case, today's task involves looking into how to check a decrementing counter to decide whether a form's elements/controls (am I using the right term here?) should be disabled. Hence the need to read up on event handling.
I'm in a hurry to find code for an on load sorta event thingy, but I'm bookmarking this page to read later... It's pretty interesting:
Introduction To Events
Oh, & this one too:
GET vs. POST
Forms. The simple yet ever enduring pain of web programming...
Yet, that is.
In any case, today's task involves looking into how to check a decrementing counter to decide whether a form's elements/controls (am I using the right term here?) should be disabled. Hence the need to read up on event handling.
I'm in a hurry to find code for an on load sorta event thingy, but I'm bookmarking this page to read later... It's pretty interesting:
Introduction To Events
Oh, & this one too:
GET vs. POST
Forms. The simple yet ever enduring pain of web programming...
Subscribe to:
Posts (Atom)