donglu5728 2015-06-05 14:19
浏览 165

单击mailto:或tel:链接时触发INSERT查询

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.

  • 写回答

1条回答 默认 最新

  • downloadTemp2014 2015-06-05 14:23
    关注

    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` ...");
    
    ?>
    
    评论

报告相同问题?