0

Hello,
I have PHP code that updates a record in my database. I need to run that code when an <a href="mailto....>
link is clicked on, before the email client runs. Thanks for the assistance.

3
Contributors
7
Replies
8
Views
5 Years
Discussion Span
Last Post by ronhymes
1

How about something like:

<a href="/mail-to.php?email=hello@world.co.uk" title="Email hello@world.co.uk">hello@world.co.uk</a>



# mail-to.php

// Retrieve email address
$email = isset($_GET['email']) ? $_GET['email'] : false;

// Validate email address?

// Update database

if($email)
    header("location: mailto:{$email}");

die('Invalid email address');

Edited by blocblue

Votes + Comments
I was going to suggest using a jQuery hook onto the mailto link to run an AJAX query, but your way is much more direct!
0

Hi blocblue, I entered:

    <a href="/mail-to.php?email=hello@world.co.uk" title="Email hello@world.co.uk">hello@world.co.uk</a>

    # mail-to.php

    <?php
    // Retrieve email address
    $email = isset($_GET['email']) ? $_GET['email'] : false;

    // Validate email address?

    // Update database
    echo "Update database";

    if ($email)
        header("location: mailto:{$email}");

    die ('Invalid email address');
    ?>

The HTML displayed:

hello@world.co.uk # mail-to.php

When executed in Dreamweaver I got:

hello@world.co.uk # mail-to.php Update databaseInvalid email address

Not sure where I am going wrong. Thanks in advance.

0

Put the HTML in your existing page and create a new PHP script containing the PHP code.

Then update the link in the HTML anchor tag to point to the correct location of the new script.

Don't just copy and paste.

0

Hi,
You are absolutely correct. It works perfectly. If I may, I had one related question. Before I inserted my php database code, I tried an echo "here" statement before the mailto and got no indication, just the email client. I then tried an echo "here", sleep(5000), echo "here" and got no indication, just the email client. Then I commented out the mailto code and when I clicked the hyperlink, 5 seconds later I saw 'herehere'. Does the code not run sequentially? I expected here, delay 5, here. Thank you very much for your assistance.

0

The header statement would perform a redirect, hence your output wouldn't be visible on the destination page.

1

Hi,

In your html:

<a href='send_email.php?email=contact@site.com'>Contact us</a>

In your php (send_email.php) :

<?php

    $email = $_GET['email']; // get the email address

    send_data_to_db(); // save data in db

    echo 'your text here !'; // echo text 

    echo '<a href="mailto:'.$email.'"> Your text here to open the mailto link</a>'; 
    // show a link to run the email client

?>

Edited by akmozo

0

Hi,
This also works perfectly. Thank you both for taking the time to resolve my issue. I appreciate your help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.