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.

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');
commented: 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! +14

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.

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.

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.

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

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

?>

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.