Sunday, November 27, 2016

jQuery: how to check any radio button in a group

Need to check a radio button in a group, i.e., where multiple buttons share the same name? Here's how.

To check the first button:
$('input[name=priority]:radio').each(function() {
  $(this).prop('checked', true);
  return false;
});

To check the nth button just track the index:
var match = 3;
$('input[name=priority]:radio').each(function(index) {
  if ( index == match ) {
    $(this).prop('checked', true);
    return false;
  }
});

Wednesday, March 12, 2014

Undefined variable problem TinyMce

I encountered this frustrating error when upgrading from v3.x of tinymce to v4. Other people have had the problem but I didn't find a solution posted anywhere.

The problem is simply resolved. Version 4.x does not play well with the advanced theme. Clear out any references to that theme from your init script and the error disappears. I would go so far as to say that when upgrading from v3 to v4 just start again from scratch.

Labels: ,

Thursday, January 04, 2007

blog.dot launch &mdash post to WordPress from Word

I have just completed and released version 1.0 of blog.dot which allows WordPress users to post to a blog directly from Microsoft Word. Download it now and turn Microsoft Word into a powerful WordPress bloging tool.
  • Produce a clean HTML export from Microsoft Word
  • Post directly to your WordPress blog from Word or use the HTML for cutting and pasting
  • Start and finish posts at your leisure without losing content
  • Create multiple posts per document
  • Submit content as posts, pages or drafts
  • Pictures, tables and passthru HTML supported
  • Insert code samples with ease.

Friday, December 01, 2006

Obfuscating email addresses on your web page

Publishing an email address "in clear" on a web page is a bad idea. It just leads to masses of spam. Here's a neat JavaScript solution that gets around the problem. The first step is to assign a JavaScript function to a hyperlink as follows:

<a href="#" onclick="javascript:sendmail();">Send an email</a>

And here's the JavaScript function that creates the email. No need for a form, which is cool:

<script language="javascript">
function sendmail() {
 /*
 we want mailto:info@restofaddress.com
 and here's how we get it...
  */
 var addr = "mailto";
 addr = addr+":";
 addr = addr+"info"+String.fromCharCode(64);
 addr = addr+"restofaddress.com";
 window.location = addr;
}
</script>

You can get more sophisticated. Supposing you want multiple addresses on a page, here's how:

<a href="#" onclick="javascript:sendmail('info', 'restofaddress.com');">
Send an email</a>

The link above passes two arguments to the function below:

<script language="javascript">
function sendmail(part1, part2) {
  /*
 we want mailto:info@restofaddress.com
 and here's how we get it...
 */
 var addr = "mailto:";
 addr = addr+part1+String.fromCharCode(64)+part2;
 window.location = addr;
}
</script>

And don't forget that you can do other things with mailto like adding a subject:

<script language="javascript">
function  sendmail(part1, part2) {
 /*
 we want mailto:info@restofaddress.com
 and here's how we get it...
  */
 var addr = "mailto:";
 addr = addr+part1+String.fromCharCode(64)+part2;
 var subj = "?subject=please send more information";
 window.location = addr+subj;
}
</script>

Thursday, October 19, 2006

MySQL DATE fields and PHP

The MySQL DATE field format is YYYY-MM-DD. So let's suppose you have a date in a text format and want to write the date to a field in MySQL database. Here are the steps: Use the PHP strtotime() function to create a timestamp:
$mydate = strtotime('31 Jan 2006');
Use the date() function to reformat the date in MySQL format:
$mydate = date('Ymd', $mydate);
Write the date (along with anything else) to the database. Note the formatting of the query in that the date value can be treated as 'text':
$sql = "INSERT INTO database.table ";
$sql .= "(adate) VALUES('".$mydate."')";
mysql_query($sql);
To retrieve a date reverse the process as follows:
$sql = "SELECT adate FROM database.table";
$rst = mysql_query($sql);
while ($arr = mysql_fetch_array($rst)) {
  $mydate = strtotime($arr['adate']);
  echo date('l, M j, Y', $mydate);
  /* will output, e.g., Tuesday, 31 Jan, 2006 */
}

Wednesday, October 18, 2006

IIS, PHP 5.1.6 and MySQL

Today I needed to upgrade an IIS web server running PHP. The current PHP version on the server was 4.4.x and I was upgrading to PHP 5.1.6. Like many before me I discovered that MySQL functions were 'broken' following the upgrade as MySQL is not enabled by default in 5.1.x versions of PHP. Getting MySQL extensions working in conjunction with this upgrade/install seems to be problematic for some and so it proved for me. I am happy to say it is now working fine. I installed and tested both the CGI and ISAPI versions on a Windows 2000. By way of testing I also converted an existing 5.1.4 CGI installation on an XP box to the 5.1.6 ISAPI version. There is a lot of conflicting advice on the net and I suspect that many problem arise from mixing and matching snippets from various postings. I strongly suggest that you read this entire article before proceeding. Pre-installation If you have tried and failed with this install already please do the following:
  1. If you have copied any of the following DLLs to the windows\system32 folder remove them: php_mysql.dll, php_mysqli.dll, libmysql.dll, libmysqli.dll.
  2. If you have copied php.ini to the windows directory (or the file already exists there) remove it.
Installation — step by step guide
  1. Download and unpack the complete 5.1.6 distribution from www.php.net.
  2. Backup or rename your existing PHP install if required.
  3. Search your system for php.ini files. If you find one in the windows directory rename it or delete it.
  4. Create a c:\php directory.
  5. Copy all the files from the downloaded distribution to c:\php.
  6. Rename phpdist.ini to php.ini.
  7. Edit the extension_dir line in php.ini to say extension_dir="c:\php\ext".
  8. Uncomment the line in php.ini that says extension=php_mysql.dll. (To uncomment the line remove the comma at the start.)
  9. Configure App Mapping for each web/virtual server using the Internet Services Manager. For the CGI install the App Mapping should be c:\php\php-cgi.exe. For the ISAPI install the App Mapping should be c:\php\php5isapi.dll. To configure App Mapping run the Internet Services Manager and display the property pages for the each website to be configured. Click the Home Directory tab then Configuration. If there isn't an existing entry for .php you will need to Add one, otherwise Edit the existing. Browse to the required executable (either php-cgi.exe or php5isapi.dll). Click OK a few times and repeat as requiired.
  10. If you are configuring IIS for the ISAPI version the php.ini file must be in the windows directory so MOVE php.ini to the windows directory — move it DO NOT copy it. If you are configuring IIS for the CGI version then php.ini can either be in your php directory OR the windows directory but NOT both.
  11. Finally, add c:\php entry to the system path. To do this double-click the System icon in Control Panel and select the Advanced tab then click the Environment Variables button. In the System variables pane double-click the Path entry and at the end of the Variable Value entry add a semi-colon followed by c:\php.
  12. Reboot and then try connecting to a MySQL database from a PHP page.

Monday, September 04, 2006

PHP header function and session variables

If you set a PHP session variable and immediately use the PHP header("Location:[url]") function to redirect to another page there may not be enough time for the session variable to be saved. This is a comon problem, especially with login scripts where the page targetted by the redirection tests for the session variable. It can lead to erratic behaviour which is difficult to diagnose. Various solutions are offered, such as the use of exit() and session_write_close() but even these in my experience are not reliable. I implemented a solution recently using PHP to set the session variable and javascript to implement the redirection. This combination works reliably.

Here is the PHP code:

<%
  session_start();
  // set a session variable
  $_SESSION['username'] = "foo";
  $url = "http://mysite/mypage.php";
%>
Then just call the javascript onload handler in the body tag and supply the $url variable as the location as follows:
onload="window.location='<% echo $url; %>';"