I'm trying to modify a class that crypts email addresses using Javascript and PHP. It works great except, the class would only crypt the email address and create the Mailto link with the email address as the clickable text. I need the class to do everything it currently does, but in addition it needs to allow the option of text that is displayed for the mailto link other than the email address. So instead of johndoe@anyonesemail.com, the mailto link could read John Doe. The text link will use alternate text, but the current problem is if I have multiple email addresses within the same PHP page, the last email text will be displayed for all email address links. I'm learning Javascript and I think the problem is the Javascript. Anyone have any ideas? Any help would be appreciated.
// testmc.php - PHP Page that creates email links //
include('class.emailcrypter.php');
$crypt = new MailCrypter();
echo "First Email: " . $crypt->addMailTo("myname@somedomainname.net","class='email'","John Doe");
echo "<br>";
echo "Second Email: " . $crypt->addMailTo("me@anydomainname.com","","Email Me");
$crypt->writeScript();
// End of PHP Page //
// class.emailcrypter.php - Email crypting class //
class MailCrypter{
var $links;
var $addlTextDesc;
//this function adds a link and returns the scrambled link text
function addMailTo($address, $addlAttributes = "", $addlTextDesc = ""){
$new = "";
$this->links[] = $address;
$this->addlTextDesc = $addlTextDesc;
for($i=0;$i<strlen($address);$i++){
$new .= chr(ord(substr($address, $i, 1)) - 1);
}
if(strlen($addlTextDesc) != 0) {
return "<a id=\"link".(count($this->links)-1)."\" href=\"mailto:$new\" $addlAttributes >$this->addlTextDesc</a>";
} else {
return "<a id=\"link".(count($this->links)-1)."\" href=\"mailto:$new\" $addlAttributes >$new</a>";
}
}
//this function echos the javascript code to descramble the links
function writeScript(){
echo "\n\n<script language=\"JavaScript\">
<!--hide from older browsers\n";
?>
function ConvLink(myLink,myText){
var temp = '';
var i;
for(i = 7; i < myLink.href.length; i++){
if (i>6){
temp = temp + String.fromCharCode(myLink.href.charCodeAt(i)+1);
} else {
//temp = temp + String.fromCharCode(myLink.href.charCodeAt(i)+1);
temp = temp + myLink.href.charAt(i);
}
}
myLink.href = 'mailto:' + temp;
<?php
if(strlen($this->addlTextDesc) == 0) {
echo "myLink.innerHTML = temp;";
} else {
echo "myLink.innerHTML = myText;";
}
?>
}
<?php
for($i=0; $i<count($this->links);$i++){
?>
ConvLink(document.getElementById('link<?=$i;?>'),'<?=$this->addlTextDesc;?>');
<?php
}
echo "-->\n</script>";
}
}
// End email crypting class //