Tech Musings

Wednesday, April 19, 2006

Javascript Validation On Multiple Select Lists

I just spent the better part of today working through a client side javascript validation challenge for our online VISIONS application. My objective was to alert a user when he or she makes a selection(s) (and/or non-selection) from a multiple drop-down select box where the choices are inputted into an array, as well as for a multiple list selection box where the choices are also placed into an array. I wanted to validate their selection to alert them if they forgot to select one of the values, or if they selected the "Other" choice from the select list but forgot to type the "Other" value in the provided text input field.

The validation was not as intuitive as I had hoped. Here's how I finally solved the first issue in the event a user forgets to select a value(s) from the multiple choice drop-down menu:

<--head-->
<--javascript-->
function check() {
if(document.form1["first_choice[]"].value == "")
{alert('Please choose a widget for Question #2!');
return false; }
return true;
}
<--/head-->


<--body-->
<--form name="form1" method="POST" action="action.php" onSubmit="return check();"-->
<--select name="first_choice[]" size="5" multiple-->
<--option value="1">widget 1<--/option>
<--option value="2">widget 2<--/option>
<--option value="3">widget 3<--/option>
<--option value="4">widget 4<--/option>
<--option value="5">widget 5<--/option>
<--/select-->
<--/form-->

For the second issue, I needed to first extract the values from the array to determine what the user selected and then alert the user if he or she selected "Other" from the list but didn't type a value in the provided "Other" text input field.

<--head-->
<--javascript-->
function check() {
{ //validation on value 5 "Other" in the subject array

len = document.form1["subject[]"].length
i = 0
chosen = ""

for (i = 0; i < len; i++) {
if (document.form1["subject[]"][i].selected) {
chosen = chosen + document.form1["subject[]"][i].value
if(chosen == "5" && document.form1.subject2.value =="")
{ alert('You still need to provide your subject area for Question 2!');
document.form1["subject[]"].focus();
return false; }
}
}
}
return true;
}
<--/head-->


<--body-->
<--form name="form 1" method="POST" action="action.php" onSubmit="return check();"-->
<--select name="subject[]" size="5" multiple-->
<--option value="1">Math<--/option>
<--option value="2">English<--/option>
<--option value="3">Science<--/option>
<--option value="4">Social Science<--/option>
<--option value="5">Other<--/option>
<--/select-->
<--input type="text" name="subject_2">
<--/form-->

I was able to "cobble" these client-side solutions together by reviewing a post and article found at the following:
http://homepage.ntlworld.com/kayseycarvey/jss3p10.html
http://forums.whirlpool.net.au/forum-replies-archive.cfm/496087.html

Tuesday, April 18, 2006

PHP, MySQL and duplicate entry error message

I recently created an online form application for our VISIONS professional development program that automatically inserts users' information into a database. The unique identifier for the application is a teacher's user number. If an application had already been submitted and was on file for that user, I wanted to return a message that read "prettier" than the standard duplicate entry 'whatever' for key some number.

So, I created a query that searched for an entry that matched the POSTED teacher id (the potential duplicate) and returned a die ("It looks like we already have an app on file...") message if the query held a value. If no entry was found, the script continue on its merry way and completed the INSERT into the database.

$colname_get_tid = "1";
$colname_get_tid = $_POST['tid'];

mysql_select_db($database_visions, $visions);
$query_get_tid = sprintf("SELECT tid, stamp FROM app WHERE app.tid = '%s'", $colname_get_tid);
$get_tid = mysql_query($query_get_tid, $visions);
$row_get_tid = mysql_fetch_assoc($get_tid);
$totalRows_get_tid = mysql_num_rows($get_tid);

if ($totalRows_get_tid) {
die ("It looks like we already have an application on file for
teacher $colname_get_tid that was submitted on $time1, $time2 at $time3. Have you completed this application before? Please call us to check on the status or your application, or click the browser's back button and try again.");
}

else do the INSERT...

Friday, April 07, 2006

PHP and file uploads

After a few hours of dedicated research, I finally figured out how to increase the PHP file upload restriction on my OS X Server. I tried a plethora of suggestions from numerous discussion boards and Web sites to no avail, mostly because the people who were posting were not fighting with this limitation issue on Apple's ridiculous flavor of Apache running under WebObjects.

After tweaking the upload_max_filesize without a hitch, I absolutely couldn't for the life of me increase the post_max_size in the php.ini file to anything greather than 8M (8 MB). Finally, I actually MOVED the directive by cutting and pasting it further down in the php.ini file next to the upload_max_filesize line. And voila! it was suddenly recognized by my server!

I also added the following directives to the very end of my httpd.conf file:

LimitRequestBody 0
php_value upload_max_filesize 75M
php_value post_max_size 75M
php_value memory_limit 35M

You direct apache to set an unlimited amount on the HTTP request body message when the LimitRequestBody equals 0. I also increased the max_execution_time to 300 seconds and the max_input_time to 260 seconds in my php.ini file, and changed the KeepAlive and timeout adjustment figures in Server Settings.

Finally, I added a hidden field named MAX_FILE_SIZE with a value of 73400320 directly before the file upload field in my html form. This field will cut off the process if someone tries to upload a file greater than 70 MB to the teacher exchange. I wrote some PHP validation which redirects the user to a message page telling them the upload failed because their file exceeded the file upload restrictions set on my server.