简体   繁体   中英

Trigger an INSERT query when mailto: or tel: links are clicked

I have a php page which is designed around mobile functionality and loads information from search criteria, information is gathered on the page and that information is then POSTed automatically to a database when the page opens to record the visit and works flawlessly, this is the code:

$mysqli = new mysqli($servername, $username, $password, $dbname);
$stmt = $mysqli->prepare("INSERT INTO `analytics_scans` (`ip_address`, `property_id`, `address`, `town`, `agent_name`, `office`) VALUES (?,?,?,?,?,?)");
$stmt->bind_param('sissss', $_SERVER['REMOTE_ADDR'], $id, $address, $town,     $agent, $office);
$stmt->execute();

On this page are 3 'buttons/images' Call, Mail and Share what I want to do is record these events into 3 separate tables exactly as in the code above without leaving the page, I can't reload it because it will record a further page visit. I can't duplicate the one shown and point to the other tables as it's the click it has to record.

Ideally something like an onClick function would be the solution but I'm sure that's not possible without leaving the page.

Any help is appreciated.

You can use Ajax (with jQuery for example).

Example:

<a href="mailto:xxx@xxx.de" onclick="return javascript:log('mail_clicked');">Write email</a>
<a href="#" onclick="return javascript:log('share_clicked');">Share</a>

In jQuery:

function log(action) {
    $.get('./log.php?action=' + action);
    return true;
}

In the log.php:

<?php

$db = new MySQLi(...);
if($_GET['action'] == "mail_clicked")
    $db->query("INSERT INTO `mail_clicked` ...");
else if($_GET['action'] == "share_clicked")
    $db->query("INSERT INTO `share_clicked` ...");

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM