<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Error Message &#187; Tutorial</title>
	<atom:link href="http://www.theerrormessage.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.theerrormessage.com</link>
	<description>Fix your error</description>
	<lastBuildDate>Tue, 13 Dec 2011 20:16:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>How to use specific language characters with PHP and MySQL (example: Romanian)</title>
		<link>http://www.theerrormessage.com/2007/12/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/</link>
		<comments>http://www.theerrormessage.com/2007/12/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/#comments</comments>
		<pubDate>Fri, 14 Dec 2007 00:54:15 +0000</pubDate>
		<dc:creator>gbl</dc:creator>
				<category><![CDATA[How to]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/12/13/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/</guid>
		<description><![CDATA[Problem: Using specific characters from European languages like Romanian, Bulgarian, Czech and so on (usually the ones without support in ISO 8859-1) rises errors when displaying the content in browsers turning special characters in unrecognizable ones. My fix for this problem is using UTF-8 character set encoding for every page of the website and the [...]]]></description>
			<content:encoded><![CDATA[<p>Problem: <em>Using specific characters from European languages like Romanian, Bulgarian, Czech and so on (usually the ones without support in ISO 8859-1) rises errors when displaying the content in browsers turning special characters in unrecognizable ones</em>.</p>
<p>My fix for this problem is using UTF-8 character set encoding for every page of the website and the MySQL tables that contain the fields you are using. Also all the html encodings from PHP use the UTF-8 character set encoding (this is not mandatory).</p>
<p>If you already have the database, but with the default character set (<em>latin1</em>) and collation <em>(latin1_swedish_ci</em>) for the tables with text fields (of type CHAR, VARCHAR, TEXT etc) in which you need to have special characters, you should change the character set of each of those tables like this:</p>
<pre class="code brush:sql">ALTER TABLE my_table CONVERT TO CHARACTER SET utf8;</pre>
<p>If you don&#8217;t have the database then you should create it and when you create a table that you need to use with specific language characters, you should specify the  character set for that table:</p>
<pre class="code brush:sql">CREATE TABLE `my_table` (
`idmy_table` tinyint(3) unsigned NOT NULL auto_increment,
`my_field` varchar(255) NOT NULL default '',
PRIMARY KEY  (`idmy_table`)
) CHARSET=utf8;</pre>
<p>The most important thing is that in PHP, after opening a database connection, before executing any query to the database, you should ensure that this code is executed
<pre class="code brush:php">mysql_query("SET NAMES utf8", $my_conn);</pre>
<p>This tells the server what character set the client is using for sending SQL statements and the character set the server should use to return the results to the client.</p>
<p>A simple example:</p>
<pre class="code brush:php">&lt;?php
$my_conn = @mysql_connect("localhost", "user", "pass")
or die("There was a problem connecting to MySQL. Please try again later.");
if(!@mysql_select_db("test", $my_conn))
{
die ("There was a problem connecting to the database. Please try again later.");
}
mysql_query("SET NAMES utf8", $my_conn);
if(!empty($_GET['mystr']))
{
// insert the string into the database
$str = htmlspecialchars($_GET['mystr'], ENT_QUOTES, "UTF-8");
$query = "INSERT INTO my_table_t (my_field) VALUES('".$str."')";
$result = mysql_query($query, $my_conn);
if($result)
{
// save the id of the table row inserted
$last_insert_id = mysql_insert_id($my_conn);
// get the last inserted value
$query = "SELECT my_field FROM my_table_t WHERE idmy_table = '".$last_insert_id."'";
$result = mysql_query($query, $my_conn);
if($result &amp;&amp; $row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$db_string = $row['my_field'] ;
}
}
}
elseif(!empty($_GET['searchstr']))
{
$str = htmlspecialchars($_GET['searchstr'], ENT_QUOTES, "UTF-8");
$query = "SELECT * FROM my_table_t WHERE my_field LIKE '%".$str."%'";
$result = mysql_query($query, $my_conn);
if($result)
{
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$search_results[$row['idmy_table']] = $row['my_field'];
}
}
}
mysql_close($my_conn);
?&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Page title&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;&lt;/head&gt;
&lt;body&gt;
&lt;?php
if(!empty($db_string))
{
echo "&lt;strong&gt;Inserted string&lt;/strong&gt;: $db_string&lt;br /&gt;";
}
?&gt;
&lt;form method="get" action=""&gt;
String to insert into the database &lt;input type="text" name="mystr"/&gt;
&lt;input type="submit" value="GO"/&gt;
&lt;/form&gt;
&lt;?php
if(!empty($search_results))
{
echo "&lt;strong&gt;Search results&lt;/strong&gt;:&lt;br /&gt;";
foreach($search_results as $id =&gt; $value)
{
echo $value."&lt;br /&gt;";
}
}
?&gt;
&lt;form method="get" action=""&gt;
Search query &lt;input type="text" name="searchstr"/&gt;
&lt;input type="submit" value="GO"/&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Tip: The search in Romanian language over the database (tested with MySQL <strong>LIKE</strong> operator) works like a charm when searching words that have special characters or not.</p>
<p>For example: In Romanian language the word &#8220;peasant&#8221; is written as &#8220;ţăran&#8221; and someone who searches it gets the same result for the search terms &#8220;taran&#8221; or &#8220;ţăran&#8221; or &#8220;ţaran&#8221; or &#8220;tărân&#8221; and so on &#8211; so this is the real magic.</p>
<p><strong>UPDATE</strong>: You may also need to add a header to the php script if you use ob_start or similar php functions like this:
<pre class="code brush:php">header("Content-type: text/html; charset=UTF-8");</pre>
<p> this usually fixes the encoding selection in Internet Explorer for this case.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/12/how-to-use-specific-language-characters-with-php-and-mysql-example-romanian/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to receive a failure notice when the recipient cannot be reached after sending an email using the PHP mail() function</title>
		<link>http://www.theerrormessage.com/2007/12/how-to-receive-a-failure-notice-when-the-recipient-cannot-be-reached-while-sending-an-email-using-the-php-mail-function-on-a-windows-server/</link>
		<comments>http://www.theerrormessage.com/2007/12/how-to-receive-a-failure-notice-when-the-recipient-cannot-be-reached-while-sending-an-email-using-the-php-mail-function-on-a-windows-server/#comments</comments>
		<pubDate>Sun, 09 Dec 2007 19:24:49 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[Apache Web Server]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/12/09/how-to-receive-a-failure-notice-when-the-recipient-cannot-be-reached-while-sending-an-email-using-the-php-mail-function-on-a-windows-server/</guid>
		<description><![CDATA[I could not receive a failure notice when sending email to an email address that does not exist ($to_address), using this code: $subject = "Email subject"; $message = "line1\r\nline2\r\nline3"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; $headers .= "To: &#60;$to_email&#62;\r\n"; $headers .= "From: Me &#60;$my_email_address&#62;\r\n"; $headers .= "Return-Path: &#60;$my_return_email_address&#62;r\n"; mail($to_email, $subject, $message, $headers); [...]]]></description>
			<content:encoded><![CDATA[<p>I could not receive a failure notice when sending email to an email address that does not exist ($to_address), using this code:</p>
<pre class="brush:php">$subject = "Email subject";
$message = "line1\r\nline2\r\nline3";
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "To: &lt;$to_email&gt;\r\n";
$headers .= "From: Me &lt;$my_email_address&gt;\r\n";
$headers .= "Return-Path: &lt;$my_return_email_address&gt;r\n";
mail($to_email, $subject, $message, $headers);</pre>
<p>The problem was that the email address I wanted to receive the failure notices to ($my_return_email_address) was not the same as the value of the configuration option sendmail_from in the php.ini file (Apache web server installed on a machine with the Windows Professional operating system). So the failure notices were sent to the sendmail_from email address if this was an existing address, instead of the email address specified in the Return-path header of the email.</p>
<p>The solution is replacing the lines:</p>
<pre class="code brush:php">$headers .= "Return-Path: &lt;$my_return_email_address&gt;r\n";
mail($to_email, $subject, $message, $headers);</pre>
<p>with:</p>
<pre class="code brush:php">// when the PHP server runs on Windows
ini_set(sendmail_from, $my_return_email_address);
mail($to_email, $subject, $message, $headers);
ini_restore(sendmail_from);</pre>
<pre class="code brush:php">// when the PHP server runs on UNIX
mail($to_email, addslashes($subject), $message, $headers, "-r $my_return_email_address");</pre>
<p>This means that, before sending the email, we set the value of the sendmail_from configuration option to $my_return_email_address and we restore the default value of the configuration option after the email is sent.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/12/how-to-receive-a-failure-notice-when-the-recipient-cannot-be-reached-while-sending-an-email-using-the-php-mail-function-on-a-windows-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to check/uncheck a bunch of checkboxes without using ids for the checkbox inputs</title>
		<link>http://www.theerrormessage.com/2007/12/how-to-selectdeselect-a-bunch-of-checkboxes-without-using-ids-for-the-checkbox-inputs/</link>
		<comments>http://www.theerrormessage.com/2007/12/how-to-selectdeselect-a-bunch-of-checkboxes-without-using-ids-for-the-checkbox-inputs/#comments</comments>
		<pubDate>Sun, 09 Dec 2007 18:21:04 +0000</pubDate>
		<dc:creator>thalissar</dc:creator>
				<category><![CDATA[HTML]]></category>
		<category><![CDATA[How to]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.theerrormessage.com/2007/12/09/how-to-selectdeselect-a-bunch-of-checkboxes-without-using-ids-for-the-checkbox-inputs/</guid>
		<description><![CDATA[My solution of checking/unchecking a group of HTML checkboxes using Javascript implies using an array of checkboxes, which means naming all the inputs of type &#8216;checkbox&#8217; like array_name[]. Example: &#60;form name="cb_form"&#62; &#60;input type="checkbox" name="cb[]" value="0" /&#62;Zero &#60;input type="checkbox" name="cb[]" value="1" /&#62;One &#60;input type="checkbox" name="cb[]" value="2" /&#62;Two &#60;input type="checkbox" name="cb[]" value="3" /&#62;Three &#60;/form&#62; In this example, [...]]]></description>
			<content:encoded><![CDATA[<p>My solution of checking/unchecking a group of HTML checkboxes using Javascript implies using an array of checkboxes, which means naming all the inputs of type &#8216;checkbox&#8217; like <em>array_name[]</em>. Example:</p>
<pre class="code brush:xhtml">&lt;form name="cb_form"&gt;
&lt;input type="checkbox" name="cb[]" value="0" /&gt;Zero
&lt;input type="checkbox" name="cb[]" value="1" /&gt;One
&lt;input type="checkbox" name="cb[]" value="2" /&gt;Two
&lt;input type="checkbox" name="cb[]" value="3" /&gt;Three
&lt;/form&gt;</pre>
<p>In this example, the name of the checkboxes array is <em>cb</em>.</p>
<p>Next, we place two links for checking/unchecking all checkboxes in our array. If someone clicks one of these links, the <em>check</em><em>All()</em> JavaScript function is called:</p>
<pre class="code brush:html">&lt;a href="" onclick="checkAll('cb_form', 'cb[]', true); return false;"&gt;Check all&lt;a&gt;
&lt;a href="" onclick="checkAll('cb_form', 'cb[]', false); return false;"&gt;Uncheck all&lt;a&gt;</pre>
<p>The checking/unchecking all checkboxes function in JavaScipt looks like this:</p>
<pre class="code brush:javascript">function checkAll(form_name, cb_name, value)
{
var cb_arr = document.forms[form_name].elements[cb_name];
// if the checkboxes exist
if(cb_arr)
{
// if the number of checkboxes is at least 2
if(cb_arr.length &gt; 1)
{
// for each checkbox
for(i = 0; i &lt; cb_arr.length; i++)
{
// check (value == true) or uncheck (value == false) it
cb_arr[i].checked = value;
}
}
else // cb_arr.length is undefined which means there is a single checkbox element that is not considered an array of one element
{
cb_arr.checked = value;
}
}
}
</pre>
<p>Note that if we only have one checkbox, the variable <em>cb</em> it is not considered an array, but a normal variable. This is useful in the situation of dinamically generated HTML pages (using PHP, for example) and the number of checkboxes varies from page to page.</p>
<p>You can test the example here:</p>
<p><script type="text/javascript"> function selectAll(form_name, cb_name, value) { var cb_arr = document.forms[form_name].elements[cb_name]; if(cb_arr) { if(cb_arr.length > 1) { for(i = 0; i < cb_arr.length; i++) { cb_arr[i].checked = value; } } else if(cb_arr.length == 1) { cb_arr.checked = value; } } } </script></p>
<form name="cb_form">
<input name="cb[]" value="0" type="checkbox" />Zero<br />
<input name="cb[]" value="1" type="checkbox" />One<br />
<input name="cb[]" value="2" type="checkbox" />Two<br />
<input name="cb[]" value="3" type="checkbox" /> Three<br />
</form>
<p><a onclick="selectAll('cb_form', 'cb[]', true); return false;" style="color: #000000; text-decoration: underline; cursor: pointer">Check all</a> <a onclick="selectAll('cb_form', 'cb[]', false); return false;" style="color: #000000; text-decoration: underline; cursor: pointer">Uncheck all</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.theerrormessage.com/2007/12/how-to-selectdeselect-a-bunch-of-checkboxes-without-using-ids-for-the-checkbox-inputs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

