We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,650 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

PHP code run from HREF tag

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
2 Days
Discussion Span
11 Months Ago
Last Updated
8
Views
Question
Answered
ronhymes
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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');
blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 13

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.

ronhymes
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 13

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.

ronhymes
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

blocblue
Practically a Posting Shark
837 posts since Jan 2008
Reputation Points: 272
Solved Threads: 161
Skill Endorsements: 13

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

?>
akmozo
Light Poster
48 posts since Jul 2012
Reputation Points: 5
Solved Threads: 9
Skill Endorsements: 0

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

ronhymes
Newbie Poster
5 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 11 Months Ago by blocblue and akmozo

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.1642 seconds using 2.68MB