Monday, November 24, 2014

Submit form in jQuery with file input and stay on the same page

You can use Ajax to send the contents of a form to the server asynchronously, so you can stay on the same page. What no one explains is how to do that if you have a file input control. The reason is that neither javascript nor jQuery has access to the contents of the file input control, just the file name. So to upload a form that contains a file input control you have to use ordinary submit, and let the browser do the file upload for you. So all you need is a regular form like:

<form action="/myserver.com/form_processing" 
    enctype="multipart/form-data" type="post">
<input type="file" name="file_element"></file>
<input type="submit"></input>
<input id="source" name="source" type="hidden" value=""></input>
</form>

Then somewhere in your javascript for the page add the source url to the hidden "source" input:

$( document ).ready(function() {
    $("#source").val(window.location.href);
});

Now on the server, in the "form_processing" script or servlet, execute a redirect to the address of the original form. In java all you need to do is:

response.sendRedirect(source);

To set the value of "source" you will have to process the multi-part request, which will require a library like Apache commons fileupload, or in php you just read $_POST, and use http_redirect to do the actual redirect. This will submit the form and it will flash, but you'll land back where you came from. No need to jump through hoops or reinvent the wheel.

Reading output

If the url you redirected to produces output you want to display you have to use a different technique. Don't use redirection on the server. Instead, just add a target="myiframe" to the form element. Then add an <iframe name="myiframe"> to the page to receive the output from the submitted-to URL. The output should be a regular HTML page. You can even add a stylesheet to it.

Monday, November 17, 2014

Ubuntu on Gigabyte P15F V2

I tried to install Ubuntu 14.04 on a Gigabyte 15F V2, i74710MQ, 8G DDR3, 1TB HD.I know that this model is sold in France with Ubuntu pre-installed. In Australia I had no such luck. After checking the warranty I decided to replace the 1TB HD with a 128GB SSD. So if it goes in for repairs I can just reinsert the original HD and pretend that I have been using Windows all along. It comes with this massive 2 year warranty, but with the proviso that if you don't use it as laid out in the user guide this will void the entire warranty. With that grain of salt swallowed I proceeded with installation. In order to get it to boot into Ubuntu 14.04 from a DVD I had first to disable the UEFI. This is a simple matter after pressing F2 during startup, and turning the UEFI into "legacy" mode. Once installed, I noticed that the wifi card didn't work. So I installed directly via an ethernet cable to my router. To get the wifi card working I had to install the new driver. (I cloned the repository, then built and installed it.) Following that I had a pretty much perfect installation. I would recommend this model if you want a Linux laptop and don't mind paying the Microsoft tax. My only gripe so far is that although the motherboard is by Gigabyte they seem to have used a Clevo shell for the laptop. And my last Clevo developed several faults just 8 months into its lifespan. But this model is more robust, if also heavier. Battery life is nowhere near that suggested elsewhere (4 hours??). I'm getting around 2 hours on a new battery, and the charger is on the big side: 120W and about 7.5x15cm. The question is: what other good Linux laptops are out there for this price? My main gripe is the lack of a backlit keyboard, which adds about $5 to the keyboard price. I really miss it when working in the dark.

On the positive side the trackpad is good, and the buttons are tolerable. The screen is very bright. In fact I have to keep having to turn it down. The keyboard is responsive and robust. It's powerful cpu-wise, and if you use the 1TB HD, also capacious.

Addendum 1: the trackpad buttons get stuck because the case gets caught in a raised position. If that happens just push the front edge of the case near the trackpad buttons down until it clicks back.

Addendum 2: The RTL8723BE driver in Ubuntu 14.04 doesn't handle bluetooth, so forget about buying bluetooth headphones for this baby. In the end, frustrated at having to use cabled earphones, I swapped it for an Intel 7260 I just had lying around (as you do), which is well supported.

Addendum 3: For Ubuntu system 15.10 I used the "live" install from a usb drive. I had many errors when booting from the device, but that USB worked perfectly in another laptop. The problem seemed to be the hardware, since it complained about the ePCI bus having a fault, and that I should check the "cabling" (?). So I removed all drives including the DVD (there's a little screw you remove in the case holding it then you can pull it out) and any auxiliary drives you installed. But in my case it required like 10 reboots before it would even run the installer. I don't know why, but every time it got a little bit further. Then you must do two things once you finally have ubuntu running: 1) run sudo apt-get update in the terminal, then run software update and 2) install the NVIDIA graphics driver "tested and proprietary" in Software Updates->Additional Drivers. Don't use the default X.Org X Server - or you will get random screen freezes. Once I did all that everything worked perfectly, but what a hell it was to get there.

Addendum 4: After 1 year three months the case is holding together. The screen hasn't fallen off, although there are a few groans in the chassis they are likely due to my falling over on concrete with the laptop in my backpack. Screws pop out now and again so it is good to have a screwdriver handy. Other than that the "structural integrity" of this cyber-space-craft is at about 70%. Not bad given how much I use it every day.

Addendum 5: The DVD drive is iffy. It works but it is overly sensitive to dvds that have some damage. The playback stops too readily after encountering an error, rather than than trying very hard to skip over damaged portions. The same DVDs in my cheap Toshiba however, usually play just fine.