简体   繁体   English

在PHP中创建一个链接到另一个页面的按钮单击计数器

[英]Creating a button click counter in php that links to another page

I have been trying to develop a button click counter that updates the number of clicks and also opens up a new page. 我一直在尝试开发一个按钮单击计数器,该计数器可以更新单击次数并还可以打开一个新页面。 I have been able to successfully get the script working on the button click counter, but my issue is that when I include an anchor tag either at the form action or on the button, the click counter stops working. 我已经能够成功使脚本在按钮单击计数器上工作,但是我的问题是,当我在表单动作按钮上包含锚标记时,单击计数器停止工作。

Here is the php script: 这是PHP脚本:

<?php
if( isset($_POST['button1']) ) { 
    incrementClickCount();
}

function getClickCount()
{
    return (int)file_get_contents("counter.txt");
}

function incrementClickCount()
{
    $counter = getClickCount() + 1;
    file_put_contents("counter.txt", $counter);
}
?>

Here is the html: 这是html:

<form action="index.php" method="POST"/>
<a href="#"><input type="button" name="button1" Value="Submit"/></a>
</form>

<div>Submitted Files: <?php echo getClickCount(); ?></div>

the a would be - a将是-

<form method="POST"/>
<input type="button" name="button1" Value="Submit"/>
</form>

Simply use header to redirect after writing. 写入后只需使用header即可重定向。

if( isset($_POST['button1']) ) { 
    incrementClickCount();
    header('location:path-to-page');
    exit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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