创建 php 函数来计算点击次数并将值存储在 cookie 中
create php function that counts clicks and stores value in a cookie
目前正在尝试将按钮和复选框的点击次数存储到 cookie 或会话中,然后将其保存到数据库中。我的想法是为每个按钮和复选框创建一个计数函数。
您可以使用 javascript XHR(ajax) 将点击数据发送到 php 脚本
要么
使用 javascript cookies 安装...
您需要添加 javascript 监控点击事件的代码,并且在每次点击时您可以触发 ajax 调用以更新数据库中的计数器。
要实现记录页面元素点击的目标,您需要使用某种形式的 http 请求与将记录点击的 PHP 服务器进行通信 - 可以是在数据库中,也可以是不稳定的会话或文件。此处的示例使用了一个简单的 ajax
函数,但您可以改用更灵活的 fetch
api 函数。
该演示应该为您提供创建记录到数据库的解决方案的基础知识...
<?php
session_start();
/* store the click in a session or log to DB */
/*
using a session will only give accurate information for a single user and a single session
so to actually record this information for all users and across time and space you really
need to use a database or, at least some sort of file.
*/
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['action'], $_POST['name'], $_POST['value'] ) && $_POST['action']=='log-click' ){
$name=$_POST['name'];
$value=$_POST['value'];
$svar='clicks';
/* create the session variable to record hits */
if( !isset( $_SESSION[ $svar ] ) ) $_SESSION[ $svar ]=new stdClass;
/* assign initial value or increment hit count */
if( !isset( $_SESSION[ $svar ]->{$name} ) )$_SESSION[ $svar ]->{$name}=1;
else $_SESSION[ $svar ]->{$name}++;
/* send something back to the ajax callback - to be processed however suits */
exit( json_encode( array(
'name' => $name,
'value' => $value,
'time' => time(),
$svar => $_SESSION[ $svar ]->{$name}
)
)
);
}
?>
<!DOCTYPE html>
<html>
<head>
<script>
/* very simple ajax function */
const ajax=function(m,u,p,c,o){
with( new XMLHttpRequest() ){
onreadystatechange=function(e){
if( this.status==200 && this.readyState==4 ){
c.call( this, this.response, this.getAllResponseHeaders(), o )
}
}
let params=Object.keys( p ).map( k=>{
return [k,p[k]].join('=')
}).join('&');
if( m.toUpperCase()=='GET' ){
u='?'+params;
params=null;
}
open( m.toUpperCase(), u, true );
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
send( params );
}
};
document.addEventListener('DOMContentLoaded',e=>{
/* Find elements of these types and bind an event listener to each */
let col=document.querySelectorAll( 'input[type="button"], input[type="checkbox"]' );
/* iterate through each DOM element and assign listener */
Array.prototype.slice.call( col ).forEach( input=>{
input.addEventListener('click', e=>{
/* construct arguments for ajax request */
let method='post';
let url=location.href;
let params={ action:'log-click', name:e.target.name, value:e.target.value };
let callback=function(r){
document.querySelector( 'output' ).innerText=r
}
let options={};
/* make the ajax request */
ajax.call( this, method, url, params, callback, options )
})
})
});
</script>
</head>
<body>
<form method='post'>
<fieldset>
<input type='button' name='bttn_1' value='Click here to win a mystery prize' />
<input type='checkbox' name='checkbox_1' value=1 />
</fieldset>
<fieldset>
<input type='button' name='bttn_2' value='Click here to win luxury items' />
<input type='checkbox' name='checkbox_2' value=1 />
</fieldset>
<fieldset>
<input type='button' name='bttn_3' value='Click here to win a car' />
<input type='checkbox' name='checkbox_3' value=1 />
</fieldset>
<fieldset>
<input type='button' name='bttn_4' value='Click here to win a dream holiday' />
<input type='checkbox' name='checkbox_4' value=1 />
</fieldset>
</form>
<output></output>
</body>
</html>
目前正在尝试将按钮和复选框的点击次数存储到 cookie 或会话中,然后将其保存到数据库中。我的想法是为每个按钮和复选框创建一个计数函数。
您可以使用 javascript XHR(ajax) 将点击数据发送到 php 脚本 要么 使用 javascript cookies 安装...
您需要添加 javascript 监控点击事件的代码,并且在每次点击时您可以触发 ajax 调用以更新数据库中的计数器。
要实现记录页面元素点击的目标,您需要使用某种形式的 http 请求与将记录点击的 PHP 服务器进行通信 - 可以是在数据库中,也可以是不稳定的会话或文件。此处的示例使用了一个简单的 ajax
函数,但您可以改用更灵活的 fetch
api 函数。
该演示应该为您提供创建记录到数据库的解决方案的基础知识...
<?php
session_start();
/* store the click in a session or log to DB */
/*
using a session will only give accurate information for a single user and a single session
so to actually record this information for all users and across time and space you really
need to use a database or, at least some sort of file.
*/
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['action'], $_POST['name'], $_POST['value'] ) && $_POST['action']=='log-click' ){
$name=$_POST['name'];
$value=$_POST['value'];
$svar='clicks';
/* create the session variable to record hits */
if( !isset( $_SESSION[ $svar ] ) ) $_SESSION[ $svar ]=new stdClass;
/* assign initial value or increment hit count */
if( !isset( $_SESSION[ $svar ]->{$name} ) )$_SESSION[ $svar ]->{$name}=1;
else $_SESSION[ $svar ]->{$name}++;
/* send something back to the ajax callback - to be processed however suits */
exit( json_encode( array(
'name' => $name,
'value' => $value,
'time' => time(),
$svar => $_SESSION[ $svar ]->{$name}
)
)
);
}
?>
<!DOCTYPE html>
<html>
<head>
<script>
/* very simple ajax function */
const ajax=function(m,u,p,c,o){
with( new XMLHttpRequest() ){
onreadystatechange=function(e){
if( this.status==200 && this.readyState==4 ){
c.call( this, this.response, this.getAllResponseHeaders(), o )
}
}
let params=Object.keys( p ).map( k=>{
return [k,p[k]].join('=')
}).join('&');
if( m.toUpperCase()=='GET' ){
u='?'+params;
params=null;
}
open( m.toUpperCase(), u, true );
setRequestHeader('Content-Type','application/x-www-form-urlencoded');
send( params );
}
};
document.addEventListener('DOMContentLoaded',e=>{
/* Find elements of these types and bind an event listener to each */
let col=document.querySelectorAll( 'input[type="button"], input[type="checkbox"]' );
/* iterate through each DOM element and assign listener */
Array.prototype.slice.call( col ).forEach( input=>{
input.addEventListener('click', e=>{
/* construct arguments for ajax request */
let method='post';
let url=location.href;
let params={ action:'log-click', name:e.target.name, value:e.target.value };
let callback=function(r){
document.querySelector( 'output' ).innerText=r
}
let options={};
/* make the ajax request */
ajax.call( this, method, url, params, callback, options )
})
})
});
</script>
</head>
<body>
<form method='post'>
<fieldset>
<input type='button' name='bttn_1' value='Click here to win a mystery prize' />
<input type='checkbox' name='checkbox_1' value=1 />
</fieldset>
<fieldset>
<input type='button' name='bttn_2' value='Click here to win luxury items' />
<input type='checkbox' name='checkbox_2' value=1 />
</fieldset>
<fieldset>
<input type='button' name='bttn_3' value='Click here to win a car' />
<input type='checkbox' name='checkbox_3' value=1 />
</fieldset>
<fieldset>
<input type='button' name='bttn_4' value='Click here to win a dream holiday' />
<input type='checkbox' name='checkbox_4' value=1 />
</fieldset>
</form>
<output></output>
</body>
</html>