Jump to content

How to check if mailto link has been pressed?


davey10101

Recommended Posts

Hi,

 

Does anyone know how to check if a mailto link has been pressed?

 

In my code I have an email link for a user but want to record if the mailto link has been pressed and add a record to a <textarea> box

<?php echo "<a href=mailto:$Email>$Email</a>";?>

 

any ideas?

 

Thanks

Link to comment
Share on other sites

Ahhh just realised my textarea is done in php....

Saving the text inside the textarea to my database is done using php...

So I can't use javascript to add a message 'onClick' to the textarea :(

 

All I want to do is just add a simple message to the textarea to say "Email Sent" when the user clicks on the mailto link..  There must be away to do this surely?

Link to comment
Share on other sites

Ahhh just realised my textarea is done in php....

Saving the text inside the textarea to my database is done using php...

So I can't use javascript to add a message 'onClick' to the textarea :(

 

All I want to do is just add a simple message to the textarea to say "Email Sent" when the user clicks on the mailto link..  There must be away to do this surely?

 

I think you have a disconnect between what PHP and JavaScript do. PHP is used to create the HTML output that is sent to the browser. The browser does not know or care "how" that HTML content is produced (flat file, PHP, ASP, etc.). JavaScript is included in the HTML code and allows functionality to occure within the browser.

 

The fact that you create the text area in PHP has no relevance on whether you can implement JavaScript. You will just nclude the JavaScript code in the output created by the PHP code. I'll post an example in a sec.

Link to comment
Share on other sites

As mjdamato said, the difference between JavaScript and PHP is that PHP is server side while JS is client side.

 

It's definitely do-able with JavaScript, but won't necessarily be reliable in the instance a user has disabled JavaScript on their browser. I'd say your best bet would be to use Ajax because it would be "behind the scenes". If you use MooTools like I mentioned earlier (or similar), it should be relatively easy.

 

If you add some more details, I should be able to come up with something, such as what else would be passed along (session id, username, etc.) to the page being called via Ajax?

Link to comment
Share on other sites

Here is an example PHP script which dynamically creates the email links and includes JavaScript to populate a textarea based on which links are clicked.

 

<?php
$contacts = array (
    'Michael' => 'michael@domain.com',
    'Jane' => 'jane@domain.com',
    'Dave' => 'dave@domain.com',
    'Linda' => 'linda@domain.com',
    'Bob' => 'bob@domain.com'
);

//Create email links
$emailLinks = '';
foreach($contacts as $name => $email)
{
    $emailLinks .= "<a href=\"mailto:{$email}\" onclick=\"addEmailToList('{$name}: {$email}');\">{$name}</a><br />\n";
}

?>
<html>
<head>
<script type="text/javascript">
function addEmailToList($value)
{
    var textareaObj = document.getElementById('emailList');
    textareaObj.value += ((textareaObj.value)?'\n':'') + $value;
    return true;
}
</script>
</head>
<body>
Send an email to the following people:<br />
<?php echo $emailLinks; ?>
<br /><br /><br />
Email links clickd:<br />
<textarea id="emailList" style="width:300px;height:150px;"></textarea>
</body>
</html>

Link to comment
Share on other sites

...won't necessarily be reliable in the instance a user has disabled JavaScript on their browser. I'd say your best bet would be to use Ajax because it would be "behind the scenes".

 

What?! JavaScript is needed to run AJAX. When the user clicks the link you already have all the information you need in the browser (i.e. JavaScript can access it). So, what benefit would there be in using AJAX to do a server-side call? You would only be having the client-side JavaScript sending information based on what the user clicked to a server page so that page can return back what? The same information it just sent?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.