Javascript – Submitting a form by hitting “Enter”

Programming,Snippets — Jeff Eske on September 6, 2013 at 9:41 am

I’m working within a commercial web-based application that uses a lot of forms, none of which can be submitted by simply hitting the Enter key.  Users were wanting to be able to at least submit a particular search form by simply hitting Enter, rather than having to click the “Search” button, so I decided to do some checking.  I found a pretty simple javascript function that can handle what I need done.  Basically, it watches key presses and when it sees the Enter key pressed (keyCode 13) it runs my search function (some_function_here).  The nice thing is that the default Search button is still available and works the same as always.

Simply add this code to the HEAD code on your page, and change the actions within the if statement to match what the current button does “onClick” and you should be good-to-go.

[code]

<script language=”Javascript”>
function detectEnterKey(e) {
var evt = e || window.event;
if(evt.keyCode == 13){
some_function_here();
}
return document.defaultAction;
}
document.onkeypress=detectEnterKey
</script>

[/code]

 

Jeff Eske

OTRS – Parsing Out the Variables in a SOAP Response

OTRS,Programming,Snippets,Web Services,Web Stuff — Jeff Eske on March 5, 2013 at 5:16 pm

Unlike some web services, the OTRS web services don’t return the values in specific xml tags; it uses generic “s-gensym” tags.  Also, it returns field name within a set of generic tags, followed by the field value in a set of generic tags.  Here’s part of the SOAP response:

<s-gensym1558 xsi:type="xsd:string">PriorityID</s-gensym1558>
<s-gensym1560 xsi:type="xsd:int">3</s-gensym1560>
<s-gensym1562 xsi:type="xsd:string">ServiceID</s-gensym1562>
<s-gensym1564 xsi:type="xsd:string" />
<s-gensym1566 xsi:type="xsd:string">Type</s-gensym1566>
<s-gensym1568 xsi:type="xsd:string">Failure</s-gensym1568>

Makes a lot of sense, doesn’t it?  Here’s an example of one that actually uses the field name as the enclosing tag name.  It makes it easier to understand:

<fusion:ServiceDown><fusion:v>false</fusion:v></fusion:ServiceDown>
<fusion:BusinessUnit><fusion:v>Some Business Unit</fusion:v></fusion:BusinessUnit>

In this example, you can see that the ServiceDown field(fusion:ServiceDown) has a value(fusion:v) of “false”.  With OTRS, every time you run your SOAP request, you some random-ass “s-gensym” field name for everything.

To actually pull the SOAP response data out, you can use a foreach() loop and dump the field names and values into a more useful array.  In addition to that, the array will be setup as a key/value pair, so you can actually get the field value by referring to the field name.  I’m sure that there’s a much better, more elegant method to do the same thing, but this way works.  First the code, then an explanation of what’s going on:

[code]
$ticketInfo = array();
$i = 0;
foreach ($TicketDetails as $name => $value){ 
 if (false !== strpos($name, "s-gensym")){
 $temp[$i] = $value; 
 $v = $temp[$i-1]; 
 if($i % 2 != 0){ 
 $ticketInfo[$v] = $value; 
 }
 $i++;
 }
}
[/code]

Basically, what this code does is take your SOAP response array ($ticketInfo) and run it through a foreach() loop.  The OTRS SOAP response includes to entries for each field.  The first entry is the field name and the second entry is the field value.  So, what I want to do is combine both entries into a key->value pair in an array.

The foreach() explodes the values of the s-gensym variables out and adds the value to 2 different arrays.  This is where the elegance is definitely lacking.  I’m first saving the value into the $temp[] array to refer back to it later.  Then, if the row is divisble by two – basically every other, or every second row, I grab the previous value from temp[] and write it to $ticketInfo[] as the key and write the current value as the value.  What I end up with in the end is an array ($ticketInfo[]) that has the field name as the key and the field value as the value.

You can then pull the desired value by referring to the appropriate key, such as:

$ticket_age = $ticketInfo[Age];
$ticket_title = $ticketInfo[Title];

A simple way to print out all of the values in your array is, again, with the foreach() loop.  All you need is this little piece of code:

[code]
foreach ($ticketInfo as $name => $value){
 echo "<b>".$name.":</b> ".$value."<br>";
}
[/code]

The foreach() will loop through the array and explode out the key/value pairs, then echo them out.  It can be handy for troubleshooting, to see what everything actually looks like in the array.

Jeff Eske

PHP – Poor Man’s Data Export/Combine Script

Programming,Snippets — Jeff Eske on February 5, 2013 at 4:50 pm

I ran into a situation a while back where I needed to merge some information from 2 separate tables into one table.   Basically, I was sort of breaking normalization rules in favor of simplicity.  I basically had a table full of helpdesk tickets with a field for customer numbers, but no customer names.  I also had a customer table that contained all of the customer information.  Since I was making a system that’s going to be “read-only” for looking up the old tickets, I wanted to be able to query a single database, when looking for the old tickets.

So, basically, I cobbled the basics of this script together to pull information from the two different tables and combine it into one single record.  I pulled the ticket record, then did a sub-search on the customer table to find the customer name in the customer table associated with the customer number from the ticket table.

Then, once that was done, I cleared the ticket table and turned around and imported my brand-spanking new csv file BACK into the ticket table, with the customer number field adjusted to be the customer name.

There’s probably an easier way to accomplish this, but when all you have is a hammer (PHP programming experience), everything looks like a nail.

Here’s the basic code.  It’s a little confusing since I’m just using generic field1, field2, etc.

[code]

<?php
#### Variables to Set
$db_host = “localhost”; // Database server name
$db_user = “db_user_username_here”; // Database Username
$db_pass = “db_user_password_here”; // Database User’s Password
$db_table1 = “your_first_table_here”; // Table name, written as database.table
$db_table1 = “your_second_table_here”; // Table name, written as database.table
$csv_filename = “csv_filename_here”; // name of the output file

#### Database Connection
$db_connect = mysql_connect($db_host, $db_user, $db_pass) or die(“couldn’t connect to DB server”);

#### Open the csv text file for writing
$csv_file_out = fopen($csv_filename,”w”); // name of the output file

#### Query the first database
$sql1 = “SELECT field1,field2,field3 FROM $db_table1”;
$result1 = mysql_query($sql1, $db_connect);

#### Pull each record from the first database then do a subquery for the related record from the second database.
while($data1 = mysql_fetch_object($result1)){ // cycle through first database’s records
// Find the record in database2 that is related to the record in database1 via field1 in both
$sql2 = “SELECT field1 FROM $db_table2 WHERE field1 = $data1->field1″;
$result2 = mysql_query($sql2, $db_connect);
// Write out field1, field2, and field3 from database1, plus field1 from database2
$write_line = $data1->field1.”,”.$data1->field2.”,”.$data1->field3.”,”.$data2->field1.”\n”;
fwrite($csv_file_out, $write_line);
}
#### Close the text file when finished
fclose($csv_file_out;
?>

[/code]

Jeff Eske

PHP – Cheesy Little PHP Script

Programming,Snippets — Jeff Eske on February 5, 2013 at 4:04 pm

Here’s a cheesy little script that parses a block of text and “linkifies” URLs.  It basically breaks the text into individual “words” and stores them in an array.  It then checks each array element to see if it appears to be a web address.  If it appears to be, it encloses address in the appropriate link code, so that it will be clickable.  If not, it simply spits it back as-is.

[code]

<?php

$content = “This is some test content.”;
#### Parse content for URLs ####
$shrapnel = explode(” “,$content); // break the text string into pieces and place them in an array.
$total = count($shrapnel); // count the pieces of shrapnel
for($i=0;$i<=$total;$i++){ // step through the array, looking at the pieces to see if they start with http
if(preg_match(“/^http./”, $shrapnel[$i])){
echo”<a href=’$shrapnel[$i]’>$shrapnel[$i]</a> “;
}
else{
echo”$shrapnel[$i] “;
}
}
?>

[/code]

You could also add a regexp for “www”, if you wanted to catch websites that don’t include the http or https.  Obviously, this has no error handling or anything else included.  It came in handy in a particular situation, so I decided to post it.

Jeff Eske

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License. | Jeff's Blog