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

时间:2016-02-09 10:31:39

标签: php html

我一直在尝试开发一个按钮点击计数器,用于更新点击次数并打开新页面。我已经能够成功地使用按钮点击计数器上的脚本,但我的问题是,当我在按钮上的表单操作中包含锚标记时,点击计数器停止工作。

这是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);
}
?>

这是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>

1 个答案:

答案 0 :(得分:0)

a将是 -

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

写完后只需使用header重定向即可。

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