Archive

Posts Tagged ‘select’

Highlight The Contents Of A Textarea With JavaScript

August 27th, 2008 No comments

If you want to give your users a little snippet of code it is nice to give them the option of selecting the entire block of code without having to highlight the text manually. This can be done with a simple JavaScript button.

Take the following form:

<form name="aForm">
<textarea name="aTextarea" cols="50" rows="20">
Copy this text onto your own website.
</textarea>
</form>

To enable the selection functionality you just need to add in an input button. The click event of this button will be to run a JavaScript bookmarklet that focuses on the textarea in question (in this case it is called aTextarea) and selects the text.

<form name="aForm">
<textarea name="aTextarea" cols="50" rows="20">
Copy this text onto your own website.
</textarea>
<br />
<input type="button" value="Highlight All" onclick="javascript:this.form.aTextarea.focus();this.form.aTextarea.select();" />
</form>

The user can then copy the text as they normally would.

Using JavaScript To Select Text

July 1st, 2008 No comments

This is a simple trick that will allow users to select the contents of a text area. First we need a text area.

<form><textarea name="textarea1" id="textarea1" rows="5" cols="40" wrap="off">This is some
long content.
This is some long content.
This is some long content.
This is some long content.
</textarea>
<br />
<input type="button" value="Select text" onclick="selectText('textarea1')">
</form>

This form also includes a button with an on click event that runs a function. This function takes a single parameter as the name of the element. It then sets the focus to this element and then selects all of the text therein.

function selectText(id){
 var id = document.getElementById(id);
 id.focus();
 id.select();
}

Why is this useful? Well lets say you had a form or a quiz that produced and answer, and you wanted people to post their answer on their blogs, which then linked back to your quiz. This would allow users to select the contents of a text area without having to select it themselves.

Categories: PHP Tags: , , , ,

Linked HTML Drop Down Menus With JavaScript

May 8th, 2008 No comments

A good usability feature of web forms is to fill the contents of one drop down menu, depending on what has been selected in a previous menu. This can be done quite easily with JavaScript.

Take the following form, in this instance I have used some server variables used in mod_rewrite, but the idea is valid.

<form name="myform" method="post">
 
Variables
<select name="variables" onchange="set_detail()">
<option value="http">-------</option>
<option value="http">HTTP Headers</option>
<option value="request">Request</option>
<option value="server">Server</option>
<option value="time">Time</option>
</select>
 
Detail
<select name="detail">
<option>------</option>
</select>
 
</form>

Notice that the onchange event of the first form calls a function called set_detail(). This is the JavaScript function used to fill out the second menu. The contents of the second menu are hard coded into the top of the form, so this sort of thing is only useful if the variables used don’t change very much.

<script type="text/javascript">
// <!--
var http = new Array('HTTP_USER_AGENT','HTTP_REFERER','HTTP_COOKIE');
var request = new Array('REMOTE_ADDR','REMOTE_HOST','REMOTE_USER','REMOTE_IDENT');
var server = new Array('DOCUMENT_ROOT','SERVER_ADMIN','SERVER_ADDR','SERVER_PORT');
var time = new Array('TIME_YEAR','TIME_MON','TIME_DAY','TIME_HOUR');
 
function set_detail(){
 var select_variables = document.myform.variables;
 var select_detail = document.myform.detail;
 var selected_variable = select_variables.options[select_variables.selectedIndex].value;
 
 select_detail.options.length=0;
 
 if(selected_variable == "http"){
  for(var i=0; i<http.length; i++){
   select_detail.options[select_detail.options.length] = new Option(http[i]);
  }
 }
 if(selected_variable == "request"){
  for(var i=0; i<request.length; i++){
   select_detail.options[select_detail.options.length] = new Option(request[i]);
  }
 }
 if(selected_variable == "server"){
  for(var i=0; i<server.length; i++){
   select_detail.options[select_detail.options.length] = new Option(server[i]);
  }
 }
 if(selected_variable == "time"){
  for(var i=0; i<time.length; i++){
   select_detail.options[select_detail.options.length] = new Option(time[i]);
  }
 }
}
// -->
</script>

Categories: JavaScript Tags: , , ,

Latest MySQL Errors With PHP

April 4th, 2008 No comments

Pinpointing where an error in a program is occurring is not always easy. This can be even more of a problem when you write an SQL query that produces an error. Take the following select statement.

ELECT * FROM table;

I tend to do this at least once a week, and don’t usually spot it until it’s too late. So when I then run mysql_query() on this statement nothing happens.

To get the error message that is produced from the SQL you can use the mysql_error() function to return the error message. The single optional parameter is the resource identifier for the MySQL connection, but if you leave this out then PHP will use the latest resource created.

echo mysql_error();

This will print the following line in your PHP output.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ELECT * FROM table' at line 1

To more intelligently print off your error message you can use the following

$error = mysql_error();
if(strlen($error) > 0){
 echo $error;
};

This code will only print off the error message if it is present.

Categories: PHP Tags: , , , , ,

Finding Duplicate Values In A MySQL Table

February 6th, 2008 No comments

To find duplicate values you need to use the MySQL COUNT() function and then pick out all of the counts that are greater than one.

SELECT value,COUNT(value) AS Count
FROM test
GROUP BY value
HAVING (COUNT(value) > 1)
ORDER BY Count DESC;

Conversely you can also select the rows that only have a single entry.

SELECT value,COUNT(value) AS Count
FROM test
GROUP BY value
HAVING (COUNT(value) =; 1)
ORDER BY Count DESC;

However, it is very nice to pick out the duplicate entries in a table, but you might still need to do something with them. Here is a query to delete any duplicate rows from a table. It does a simple self join and deletes the row value with the lowest ID.

DELETE bad_rows.*
FROM tests AS good_rows
INNER JOIN tests AS bad_rows ON bad_rows.number = good_rows.number
AND bad_rows.id > good_rows.id;

More information on this deletion query and other methods of deleting duplicates can be found at Xaprb.com.

Categories: MySQL Tags: , , ,