Javascript auto-submit form

I was working on this for a new site idea (not for this one, it’s be a whole new site) and made this, it’ll defiantly come in useful to me later, probably to others too. What this little function does is submit the data using ajax whenever the as() function is called. This can be used in a few ways, the two more obvious, and probably most useful, ones are having a form submit button ass the as() function to submit the data without having to reload or redirect the user, the other is the method I’m using.

I’m using this to automatically submit the form whenever it is updated, so i’ve placed in on the forms keyup event. That way, when something is typed in any part of the form it’s sent to the server for an update. When using this method you’ll want to make sure your save code is very lightweight (I suggest checking the form contents meet any requirements before sending it and having the form just update the data near blindly (better run a check for injection hacks but thats it), and of couse not doing that on anything where data loss is going to cause headaches). Anyways enough babble, here’s the code


Things to note about the code. I set the first variable sent manually (on line 3) if you don’t want one set by the script then you can remove that line but you’ll have to remove the first “&” or you’ll get some unexpected events (probably, I wouldn’t try it). If you want to use a GET instead of the POST then you’ll have to re-jigger a bit, I can do if for you if you want just leave a comment. Also, as with a lot of my stuff, no IE to test on. Should work though (I’ve made sure not to repeat thing I know don’t work in IE), if someone tries it and it fails let me know (and with what error) and I’ll fix it.

Update: 11/24/10 1:51am
Removed an alert that did nothing but annoy, changed the line reference accordingly.

Update 12/29/10 11:10pm
Added a example of this in action to the example page. It’s a pretty simple example but shows it working, I can always come up with a better one if it’s not clear enough or if it’s not useful for a particular application your trying.

PHP absolutely relative links

After there were some issues importing a WordPress site from a live server to a local server so we could work on it without fear of it breaking (it needs some pretty extensive back-end reorganization) I decided to solve the problem of mixed absolute and relative links my simply making them all relative. This works great in theory but in practice has some problems, for instance some of the classes call, and are called, from multiple location in the directory tree. PHP can handle this but when your trying to include an HTML image or javascript that can be a problem. Lets see if I can explain this problem a little better.

You have a class with a function that generates a form, it is stored on the root level as forms.class.php. You call this form from multiple locations, on the signup page also on the root directory located at signup.php and on the account modification page located at /account/index.php. The function uses a javascript file that is located at /js/form.js. If you call that file in the forms.class.php function via js/form.js it will work great on your signup.php file on the root of your server, but on /account/index.php it will 404, not being able to find it. Thats what this bit of code handles.


Place this on the top of the file thats displaying and then you can use the PHP definition ROOT in the class for the javascript file. On the root level of the server ROOT will be blank, and the link will still work just fine, but in the account/index.php page ROOT will be ../ which will tell the browser to look back one directory and then it will be able to find the file.

This is still a pretty sparsely tested little clip (just around a few folder in a test server) and there’s probably a better way to do it, but until I get a chance to sit down with the WordPress installation at work and test it out it is what it is. There are other improvements that can be done (like making it a function so your not locked into the current value making it not always go back to root) but for now it’s doing what I wanted it to do. I’ll make some improvements to it and update the snippet here and and snipplr accordingly.

Also, I’m sure theres a better way to do this, this is just the first solution that came to mind and I decided to roll with it. If there is a simpler method (not involving /htaccess or anything link that, simple PHP) that I find to do this then I’ll update accordingly