Friday, October 16, 2009

PHP webform emails XML plain text

The PHP code that generates an email is so short due to using foreach($_POST as $key => $value). I was able to use the key names because, in the HTML form, I named the input fields with full, readable, descriptive names ( i.e. first_name rather than fname ).

The email is encoded as plain text. I originally set it up to send an ugly output from the form. Then the users requested XML. It was amazing how simple that was to add. I'd recommend XML as the structured output, oddly, for the readability alone.

SIMPLEST XML EXAMPLE
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
 <to>Tove</to>
 <from>Jani</from>
 <heading>Reminder</heading>
 <body>Don't forget me this weekend!</body>
</note>
<?php
$recipient = "this.is.lance.miller@gmail.com";
$message = "";
$ip = $_SERVER['REMOTE_ADDR'];
$email = "this.is.lance.miller@gmail.com";
if ($_POST['email']) { $email = $_POST['email'] ; }
if ($_POST['submit']) {
$message = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<!-- html/php/xml code by this.is.lance.miller@gmail.com -->\n";
$message .= "<volunteer>\n";
foreach($_POST as $key => $value) {
 if ($key != "submit") { $message .= "\t<$key>\n\t$value\n\t</$key>\n";}
                                  }
$message .="\t<remote_address>\n\t${ip}\n\t</remote_address>\n";
$message .= "</volunteer>\n";
mail( $recipient , "Volunteer Form Submission $email $ip", $message, "From: $email" );
echo "Thank you. Your information has been sent.";
} else {

?>
<h1>Volunteer Application Form</h1>
<div style="padding:5px;background:#CCCCCC">
<form name="volunteer_form" method="POST" action="<?php echo $PHP_SELF;?>">
CONTACT INFORMATION:
<br />
First Name:<input type="text" name="first_name" size="25" />
Last Name:<input type="text" name="last_name" size="25" />
<br />
Address:<input type="text" name="address" size="80" />
<br />
Email:<input type="text" name="email" size="80" />
<br />
Home Phone:<input type="text" name="home_phone" size="16" />
Other Phone:<input type="text" name="other_phone" size="16" />
<br />
Emergency Contact:<input type="text" name="emergency_contact" size="25" />
Phone:<input type="text" name="emergency_contact_phone" size="16" />
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
AVAILABILITY:
<br />
<table><tr>
<td valign="top" align="left">
Weekdays <input type="radio" name="weekdays" value="Yes"> Yes <input type="radio" name="weekdays" value="No"> No 
<br />
Weekends <input type="radio" name="weekends" value="Yes"> Yes <input type="radio" name="weekends" value="No"> No 
<br />
Evenings <input type="radio" name="evenings" value="Yes"> Yes <input type="radio" name="evenings" value="No"> No 
<br />
</td><td valign="top" align="right">
<textarea cols="60" rows="4" name="details">
Details here if needed: 
</textarea>
</td></tr></table>
<br />
What events are you interested in volunteering for at the Abbotsford Entertainment and Sports Center?
<br />
<input type="radio" name="concerts" value="Yes"> Concerts
<input type="radio" name="hockey" value="Yes"> Hockey
<input type="radio" name="theatre" value="Yes"> Theatre
<br />
<input type="radio" name="trade_shows" value="Yes"> Trade Shows
<input type="radio" name="private_events" value="Yes"> Private Events
<br />
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
SKILLS AND EXPERIENCE:
<br />
Do you have a valid Drivers License?
<input type="radio" name="driverslicense" value="Yes"> Yes <input type="radio" name="driverslicense" value="No"> No 
<br />
Do you have a first aid certificate?
<input type="radio" name="first_aidc_ertificate" value="Yes"> Yes <input type="radio" name="first_aid_certificate" value="No"> No 
<br />
Do you have a food safe certificate?
<input type="radio" name="food_safe_certificate" value="Yes"> Yes <input type="radio" name="food_safe_certificate" value="No"> No 
<br />
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
SPECIAL SKILLS:
<br />
<input type="radio" name="av_support" value="Yes"> A/V Support
<input type="radio" name="artistic_skill" value="Yes"> Artistic Skill
<input type="radio" name="child_care" value="Yes"> Child Care
<br />
<input type="radio" name="coach_referee_official" value="Yes"> Coach/Referee/Official
<input type="radio" name="customer_service" value="Yes"> Customer Service
<input type="radio" name="first_aid_certificate" value="Yes"> First Aid Certificate
<br />
<input type="radio" name="public_speaking" value="Yes"> Public Speaking
<input type="radio" name="office_skills" value="Yes"> Office Skills
<input type="radio" name="photography" value="Yes"> Photography
<br />
<input type="radio" name="musical_skills" value="Yes"> Musical Skills
<input type="radio" name="sport_skills" value="Yes"> Sport Skills
<input type="radio" name="teaching_education" value="Yes"> Teaching/Education
<br />
<input type="radio" name="writing_editing" value="Yes"> Writing/Editing
<input type="radio" name="other_skill" value="Yes"> Other <input type="text" id="other_skill_detail" size="25" />
<br />
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
<input type="submit" name="submit" value=" SEND " />
</form>
</div>
<div style="padding:2px">
</div>
<div style="padding:5px;background:#CCCCCC">
<span style="font-style:monospace;font-size:75%">this.web.form by this.is.lance.miller@gmail.com</span>
</div>
<?php
}
?>

Wednesday, October 14, 2009

OS X CLI Open and Kill an App

#!/usr/bin/env bash

case ${1} in
      "open")
      open /Applications/Yahoo\!\ Messenger.app/;
      let "myentry = `ps -au lance | grep /Applications/Yahoo\!\ Messenger.app/ | grep -v grep | awk '{print $2}'`";
      echo "press enter to kill ${myentry}";
      read;
      kill $myentry; 
      clear;
      ps -au lance | grep /Applications/Yahoo\!\ Messenger.app/ | grep -v grep; 
       exit;;
   "") ls $0;cat $0;exit;;
esac


Monday, October 5, 2009

ipserver.py

#!/usr/bin/env python
#
# 
import os
import cgi


def main():
 print 'Content-Type: text/plain'
 print ''
        print os.environ['REMOTE_ADDR']
   

if __name__ == "__main__":
        
 main()

Thursday, July 2, 2009

javascript return highest

function returnhighest(input) {
var donothing = 0
var temp = 0
var highest = 0
for (var i=0;i < idtotalarray.length;i++) { input == idrarray[i] ? temp=idtotalarray[i] : donothing=donothing 
                                        temp > highest ? highest=temp : donothing=donothing }       
return highest      
}

Tuesday, June 30, 2009

Select option list access with Javascript

function populateselection(input) { 
for (i=0;i < input.options.length; i++) 
{ input.options[i].selected ? chosenoption=input.options[i].value : donothing=donothing }

window.status=chosenoption 

}

<form name="selectinterface">
<select name="refererselect" onchange="populateselection(selectinterface.refererselect)">
<option name="topreferer" value="nothing">view stats per referer</option>
{% for referer in refererlist %}
<option name="{{ referer }}" value="{{ referer }}">{{ referer }}</option>
{% endfor %}
</select>
</form>

Saturday, April 18, 2009

Great narrative on compression

Practical Common Lisp: Designing a Domain-Specific Language

"Designing an embedded language requires two steps: first, design the language that'll allow you to express the things you want to express, and second, implement a processor, or processors, that accepts a "program" in that language and either performs the actions indicated by the program or translates the program into Common Lisp code that'll perform equivalent behaviors.

So, step one is to design the HTML-generating language. The key to designing a good domain-specific language is to strike the right balance between expressiveness and concision. For instance, a highly expressive but not very concise "language" for generating HTML is the language of literal HTML strings. The legal "forms" of this language are strings containing literal HTML. Language processors for this "language" could process such forms by simply emitting them as-is.

(defvar *html-output* *standard-output*)

(defun emit-html (html)
  "An interpreter for the literal HTML language."
  (write-sequence html *html-output*))

(defmacro html (html)
  "A compiler for the literal HTML language."
  `(write-sequence ,html *html-output*))

This "language" is highly expressive since it can express any HTML you could possibly want to generate.1 On the other hand, this language doesn't win a lot of points for its concision because it gives you zero compression--its input is its output.

To design a language that gives you some useful compression without sacrificing too much expressiveness, you need to identify the details of the output that are either redundant or uninteresting. You can then make those aspects of the output implicit in the semantics of the language.

For instance, because of the structure of HTML, every opening tag is paired with a matching closing tag.2 When you write HTML by hand, you have to write those closing tags, but you can improve the concision of your HTML-generating language by making the closing tags implicit."