Today I was just doing more website work. Mostly I was trying to get some javascript working on a form again. Since I have now changed the structure of the form, the javascript couldn't work as the ids etc. of the elements it was trying to access didn't exist.
It took me a long time to get it working, and it is pretty messy code-wise. When I have more time I should probably rewrite all the javascript for the site, maybe using jquery instead of plain js.
Yesterday I had a problem when I wanted to sort an database result set in alphabetical order, but many of the records had NULL for the field I wanted to sort on. This meant that all the rows with NULL for the field being sorted on came at the start of the result set, when I wanted them at the end. There are quite a few different solutions here: MySQL Orderby a number, Nulls last.
In the end for my solution, I just used IFNULL(field, 'Unknown')
for the field I wanted to sort on. This worked best for my particular situation.
Today I had an issue where I needed to get the URL of the current page in PHP, minus the query string. I found there is actually a PHP function you could use to do this: parse_url. However, this returns quite a bit of information. I thought that it might actually be faster to just do substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?'))
, so I am using that instead (and storing the result in $_SERVER['REQUEST_URI_NO_ARGS']
).
When doing the javascript validation of the form, one of the problems I had was accessing the form elements that used arrays in their names (which was all of them). W3 schools gives an example of accessing a form element using js based on the name, but no examples where the name contains an array: W3 Schools - JavaScript Form Validation.
I found the answer to my problem here: HTML form input tag name element array with JavaScript. The answer is that the name attribute is just treated as a string in javascript. So while a form element with a name of records[27][country][123][name]
would be accessed as $_POST['records'][27]['country'][123]['name']
in PHP, in js it is just accessed as form.elements['records[27][country][123][name]']
.
While working on the javascript, I had to access the last element of an array. I was doing this using var lastItem = myArray[myArray.length-1];
. I thought that there must be a method of the Array object that can be used to get the last element instead (like PHP's end()
). But it turns out that there isn't: last element of array in javascript.
Another problem I had was trying to find specific nodes in a section of HTML cloned from the page, but not yet inserted back into the document. In the end I didn't need this, but there is some info on this here: Is there any way to find an element in a documentFragment?.
Firefox with its autocomplete refilling form fields with previous values when a page is refreshed struck again. This time I noticed the issue with a hidden field. Luckily I spotted it, and so added autocomplete="off"
to the hidden fields. Depending on what actions the user had taken, these hidden fields could have included references to data that was no longer existing in the form once the page had been refreshed, and so could have caused some problems. More about this issue here: How can I prevent Firefox's Autocomplete?.
The last issue I had was with checking the type of an object. I thought I remembered that typeof
would give 'object' for an array. I didn't actually need this, as I knew my data would only be a string or an array, so I could just check for typeof myvar == 'string'
and if not assume it was an array. But there is more info on this here: Finding Variable Type in JavaScript.
Stack Overflow is a really great resource, it often comes up with answers to my questions in the search results. Probably the best thing about it though is that you usually get opinions from a few different people rather than just one person's way of doing something. This allows you to choose the best method that works for you.
No comments:
Post a Comment