Selecting a <option> in a <select> with a given value.

April 30, 2010 by paulhenry · Comments Off
Filed under: Javascript Tutorials, Tutorials 

Have you ever needed to auto-select a specific <option> in a <select> with PHP but didn’t want to generate the whole <select> or go through a complex selecting process?

Here is some Javascript code that will set the first <option> of a <select> with a given value to selected.


function selectItem(name, value) {
  if(value == "") {
    return;
  }
  var selectElement = document.getElementById(name);

  var options = selectElement.getElementsByTagName('option');

  for(var i = 0; i < options.length; i++) {
    if(options[i].value == value) {
    options[i].selected = true;

    return;
    }
  }

  return;
}

Say you had the following select, and you wanted to select a certain element say if the something went wrong when the user submited the form. You could call the above function to select the state of Texas like so: selectItem("states", "TX"). The first argument is the id of the select, and the second is the value you want to select.

<select name="State" id="states">
  <option value="" selected="selected">Select a State</option>
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
  <option value="AR">Arkansas</option>
  <option value="CA">California</option>
  <option value="CO">Colorado</option>
  <option value="CT">Connecticut</option>
  <option value="DE">Delaware</option>
  <option value="DC">District Of Columbia</option>
  <option value="FL">Florida</option>
  <option value="GA">Georgia</option>
  <option value="HI">Hawaii</option>
  <option value="ID">Idaho</option>
  <option value="IL">Illinois</option>
  <option value="IN">Indiana</option>
  <option value="IA">Iowa</option>
  <option value="KS">Kansas</option>
  <option value="KY">Kentucky</option>
  <option value="LA">Louisiana</option>
  <option value="ME">Maine</option>
  <option value="MD">Maryland</option>
  <option value="MA">Massachusetts</option>
  <option value="MI">Michigan</option>
  <option value="MN">Minnesota</option>
  <option value="MS">Mississippi</option>
  <option value="MO">Missouri</option>
  <option value="MT">Montana</option>
  <option value="NE">Nebraska</option>
  <option value="NV">Nevada</option>
  <option value="NH">New Hampshire</option>
  <option value="NJ">New Jersey</option>
  <option value="NM">New Mexico</option>
  <option value="NY">New York</option>
  <option value="NC">North Carolina</option>
  <option value="ND">North Dakota</option>
  <option value="OH">Ohio</option>
  <option value="OK">Oklahoma</option>
  <option value="OR">Oregon</option>
  <option value="PA">Pennsylvania</option>
  <option value="RI">Rhode Island</option>
  <option value="SC">South Carolina</option>
  <option value="SD">South Dakota</option>
  <option value="TN">Tennessee</option>
  <option value="TX">Texas</option>
  <option value="UT">Utah</option>
  <option value="VT">Vermont</option>
  <option value="VA">Virginia</option>
  <option value="WA">Washington</option>
  <option value="WV">West Virginia</option>
  <option value="WI">Wisconsin</option>
  <option value="WY">Wyoming</option>
</select>

Hope this helps! :)

How to format PHP date() for MySQL’s DateTime DataType

March 11, 2010 by paulhenry · Comments Off
Filed under: PHP Tutorials, Tutorials 

This is a simple call to a function like so:

Get the current time:
$time = date("y-m-d H\:i\:s");

Or get the current time from a timestamp, set to one week from the current time:
$nextWeek = time() + (7 * 24 * 60 * 60);
$time = date("y-m-d H\:i\:s", $nextWeek);

Installing MySQL Workbench from Source on Fedora 12

March 6, 2010 by paulhenry · Leave a Comment
Filed under: Linux Tutorials, Tutorials 

Here is a quick tutorial on how to install MySQL Workbench on Fedora 12, as there are some unique things you need before it will install correctly.

First, download the source code tar ball from dev.mysql.com here: http://dev.mysql.com/downloads/workbench

Next, unpack the tarball into a directory of your choice, open a terminal, and cd into that directory where you unpacked the source.

You will need root priviliges for the install so you can do so by running either su or sudo -s.

You will need to run the following command to install the librarys required by MySQL Workbench.
yum install libtool
libzip libzip-devel glib2 glib-devel2 libxml2 libxml2-devel libsigc++20 libsigc++20-devel libglade2 libglade2-devel gtkmm24 gtkmm24-devel libgnome libgnome-devel boost boost-devel libuuid libuuid-devel lua lua-devel pcre pcre-devel mysql mysql-devel

Next run sh autogen.sh

Then make

And then finally make install

If the whole process succeeded, the MySQL Workbench binary should be /usr/local/bin/mysql-workbench

Sending POST parameters via HTTP to a server in C#

December 21, 2009 by paulhenry · Comments Off
Filed under: C# Tutorials, Tutorials 

Here is a C# Method for sending data to a server via POST.

You would call this method like this:
get_post_response("http://somesite/", "email=email&password=password")

If any of the characters in the params contain special characters (Such as $, #, @) you must call HttpUtility.UrlEncode() on each param or on the entire param string. Here is how you do that: http://www.vcskicks.com/encode-url.php

public static string get_post_response(string page_url, string page_params) {
  var request = (HttpWebRequest)HttpWebRequest.Create(page_url);
  request.CookieContainer = new CookieContainer();
  request.CookieContainer.Add(session);
  request.ContentType = "application/x-www-form-urlencoded";
  request.Method = "POST";
  byte[] bytes = Encoding.ASCII.GetBytes(page_params);
  request.ContentLength = bytes.Length;
  using (Stream os = request.GetRequestStream()) {
     os.Write(bytes, 0, bytes.Length);
  }

  var resp = request.GetResponse();

  CookieContainer cookieJar = new CookieContainer();

  session = cookieJar.GetCookies(request.RequestUri);

  Stream respStream = resp.GetResponseStream();

  byte[] buffer = new byte[1024];
  string text = "";

  while (Convert.ToBoolean(respStream.Read(buffer, 0, buffer.Length)))
     text += Encoding.ASCII.GetString(buffer).Trim('\0');

  return text;
}

Enjoy!

How to install Adobe Flash 10 on Slackware 13 for Firefox

December 7, 2009 by paulhenry · Comments Off
Filed under: Linux Tutorials 

The installation of adobe flash on Slackware is quite a simple process actually. It requires 4 steps.

First, open a terminal then type su to get a root shell.

Next run the following command to get the latest version of adobe flash 10 from adobe.
root@paulhenry-laptop:~# wget http://fpdownload.macromedia.com/get/flashplayer/current/install_flash_player_10_linux.tar.gz

Next uncompress the file you just downloaded.
root@paulhenry-laptop:~# tar -zxvf install_flash_player_10_linux.tar.gz

Then simply copy the uncompressed library file to the /plugins directory of firefox.
root@paulhenry-laptop:~# cp libflashplayer.so /usr/lib/firefox/plugins/

Restart (or start) firefox and look in Tools->Addons->Plugins and Shockwave flash should be installed!

Feel free to email me at paulhenry@mphwebsystems.com if you have any problems.