I-Am-Bot Code, technology and life

7May/090

Javascript to generate query from form

Posted by Srinath

One of the most mundane tasks in web development is insertion of forms into databases. Writing a query for each form is cumbersome and isn't the best way to go about it. Now before I get started, there are "far better" ways of doing it like using datagrid, or form designers, etc. Since I don't find them suitable for my taste, I've put together a very simple javascript that can do the job.

My example is based on PHP/MySQL and uses jQuery

The Problem: Generating an Insert Query for the particular form. Traditionally, if you form had say 25 fields, you'll name them, and post/get them to a server side script like php. Then you have to get each field using the $_POST or $_GET variables, and generate the query. Imagine doing this for 25 fields! Hope you get the point.

The Solution: Automatically traverse the form, generate the query using javascript, then submit it to the server side script by Ajax POST using jQuery. The function takes 3 arguments: table name, success message, error message.

The Limitations: As you'd imagine, there are a few serious limitations:

  • The order of the form elements should match the order of the fields in the table. Needless to say the number of form elements should also match the number of fields in the table.
  • The database name is hard coded into the server side script. Since my work involves only a single database, I find it easier to keep things simple.

Steps:

1. In your normal HTML form, arrange the input boxes in the order that they appear in your database table. In this example, we have 3 text boxes: user, email and pass.

<div id="container">
<form id="loginForm" method="POST" action="dbprocess.php">
<dd>
<dl><label for="user">Username</label></dl>
<dt><input type="text" name="user" maxlength="20" /></dt>
</dd>
<dd>
<dl><label for="email">Email</label></dl>
<dt><input type="text" name="email" maxlength="20" /></dt>
</dd>
<dd>
<dl><label for="pass">Password</label></dl>
<dt><input type="password" name="pass" maxlength="8" /></dt>
</dd>
<input type="submit" value="Submit" id="submitForm" />
</form>
</div>

2. Include the jQuery.js and formSubmit.js files in the header

3. Insert the following Javascript code that disables the submit button of the form, and calls our formSubmit function instead. The form submission is handled in the function via jQuery Ajax request.

<script type="text/javascript>

$(document).ready(function(){
$("#loginForm").submit(function(){
return false;
});
$("#submitForm").click(function(){
formSubmit("users","New user added successfully!","Error adding user!");
});
});
</script>

4. Write your server side PHP script to connect to the database, and perform the operation

<?php
$query = stripslashes($_POST["query"]);

if(!$con = mysql_connect("localhost","root",""))
die("Database Connection Error");

if(!mysql_select_db("login"))
die("Database does not exist");

$result = mysql_query($query,$con);
if($result)
echo "1";
else
echo mysql_error($con);
mysql_close($con);
?>

And that's it! Your form insertion should be working provided you took care of the limitations. This script is useful if you have many forms with lots of input fields in your application. Comments, suggestions and criticisms welcome!

Download the script with example

14Feb/091

Openslide – Open source alternative to slideshare

Posted by Srinath

Just a quick heads up. One of the nice guys in India has floated a open source project to create an alternative to http://slideshare.net. The project is in the planning stage, so if anyone wants to help, you can contact the admin.

Read more about the project, straight from the guy who started it on his blog here

The sourceforge page is here

Tagged as: , , 1 Comment
2Feb/095

dScript reaches 200 downloads!

Posted by Srinath

And i never expected it to. I know this is insignificant compared to all the other sutff out there on the internet, but hey, it counts for me. So thanks to everyone who downloaded, and hopefully using it.

As a bonus, I'm working on another version of the script, which has a slightly different interface. Mac users can guess what its about. Feature wise its pretty much the same, but the interface is polished. Hopefully it will be out sometime this month.

P.S: I would really like to get some feedback from users on this one. Its been kind of disappointing that way. So if your using it, or have anything to say, I'm always listening.

Kampai!

Tagged as: , 5 Comments
31Jan/099

dScript – php file download counter script

Posted by Srinath

Update: A minor bug, which breaks the (+) icon next to a folder when no files/folders are present in the parent directory fixed. Thanks to Przemysław Kłys for reporting it. Compressed the JS file for smaller package size. Cleaned up a few lines. As usual get it here

After the first releaseof my simple php download counter script, I present a new improved
version! It is a complete rewrite, with a better interface and cleaner
code.Its written in php/javascript/css.

You can watch a demo here.

Minimum Requirements :

A web server that supports php. User should have write permissions to the directory.

Installation Notes :

1. Download
2. Unzip in your webserver http://example.com/path
3. chmod /path/Includes/log to 777
4. Drop files/folders and use!
5. Directly link files as http://example.com/path/download.php?fname=<./path to file>

Features :

1. Tree like listing of all files and folders within the directory.
2. No hassles with changing folders. Just expand the folder, and click on file.
3. No database required. All stats are stored in log file.
4. Icons displayed for most file types.
5. XHTML and CSS valid code.

Tested with Firefox 2,3 Opera, Chrome, Safari, and IE 7.

Bugs :

1. Displaying file size and downloads for nested files is broken. Needs formatting.

Credits :

Read file readme.txt

Terms:

Should work reasonably well on most servers. No guarantees
whatsoever! You can modify and distribute as you like. But please make
sure the readme.txt file is present when you distribute it.

All comments and suggestions welcome. Email : srinath [AT] iambot [DOT] net

Tagged as: , , 9 Comments