我正在创建一个显示和广告的WordPress小部件。点击广告后,我想用一些细节记录点击次数。
已经创建了数据库表,并且可以使用f1_add_advert_click($advert_id)
中的save-advert-click.php
函数正确保存条目,<script type="text/javascript">
function myAjax() {
$.ajax({
type: "POST",
url: './wp-content/plugins/facilitaire-advert-widget/save-advert-click.php',
data:{action:'call_this'},
success:function(html) {
alert(html);
}
});
}
</script>
与我想要调用它的Widget的PHP位于同一目录中from(插件的根目录)。
要求:
我已经在Stackoverflow上看到并尝试了很多例子,但要么我没有得到它,要么那里的情况与我的不同。很高兴欣赏一个在我的情况下适用的评论良好的代码示例。请注意,我不是一名经验丰富的程序员,特别是“绿色”&#39;谈到JavaScript和jQuery。
根据我在网上看到的内容,我认为我应该首先使用AJAX注册点击,然后将浏览器发送到目标页面。根据{{3}}的底部答案,我不确定是否必须使用onclick。
到目前为止,这部分已经投入了大约四个小时。现在寻求帮助。要显示的代码不多,因为我删除了所有无效的代码。这是我目前在插件的widget功能中得到的(省略了常规php显示标题等):
<p><a href="" onclick="myAjax()" class="deletebtn">Link of Advert</a>
后面跟着链接和点击操作
// check for action signal
if($_POST['action'] == 'call_this') {
f1_add_advert_click ();
}
/**
* Records a click event in the database
*/
function f1_add_advert_click ()
{
$advert_id = random_int(1,300); // change to $clicked_advert_id later
$user_ip = get_user_ip();
global $wpdb;
$table_name = $wpdb->prefix . 'f1_advert_clicks';
$wpdb->insert( $table_name, array(
'advert_id'=> $advert_id,
'user_agent'=> $_SERVER['HTTP_USER_AGENT'],
'ip_address'=> $user_ip
),
array(
'%d',
'%s',
'%s'
)
);
}
/**
* Get user IP
*/
function get_user_ip()
{
$client = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote = $_SERVER['REMOTE_ADDR'];
if ( filter_var($client, FILTER_VALIDATE_IP) ) {
$ip = $client;
} elseif ( filter_var($forward, FILTER_VALIDATE_IP) ) {
$ip = $forward;
} else {
$ip = $remote;
}
return $ip;
}
然后这些是我的内容save-advert-click.php
show-advert.php
使用解决方案更新
感谢@ mplungjan的建议,我将我的代码更新为以下工作代码。请注意与@ mplungjan的代码有些不同,因为我必须做一些调整才能使它在WordPress中运行。
<script>
jQuery(function() {
jQuery(".advertLink").on("click",function(e) {
e.preventDefault(); // stop the link unless it has a target _blank or similar
var href = this.href;
var id= <?php echo $advert_id; ?>;
jQuery.post("<?php echo get_home_url(); ?>/wp-content/plugins/facilitaire-advert-widget/save-advert-click.php",
{ "action":"register_click", "advert_id":id },
function() {
location=href;
}
);
});
});
</script>
<a href="https://stackoverflow.com/" id="advert1" class="advertLink">Link of Advert</a><br />
<?php
save-advert-click.php
然后在<?php
// check for action signal
if($_POST['action'] == 'register_click') {
f1_add_advert_click( $_POST["advert_id"] );
}
/**
* Records a click event in the database
*/
function f1_add_advert_click ( $advert_id )
{
$user_ip = get_user_ip();
// need to load WordPress to be able to access the database
define( 'SHORTINIT', true );
require_once( '../../../wp-load.php' ); // localhost needs this, production can have require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );
global $wpdb;
$table_name = $wpdb->prefix . 'f1_advert_clicks';
$wpdb->insert( $table_name, array(
'advert_id'=> $advert_id,
'user_agent'=> $_SERVER['HTTP_USER_AGENT'],
'ip_address'=> $user_ip
),
array(
'%d',
'%s',
'%s'
)
);
}
的顶部我有
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle: NSNumberFormatterCurrencyStyle];
costN = [NSNumber numberWithFloat: myTextField.text.floatValue];
gameN = [NSNumber numberWithFloat: cost * 0.10];
steamN = [NSNumber numberWithFloat: cost * 0.05];
breakevenNumber = [NSNumber numberWithFloat: (costN.floatValue + gameN.floatValue + steamN.floatValue)];
breakevenS = [formatter stringFromNumber: breakevenNumber];
breakevenDisplay.text = [NSString stringWithString: breakevenString];
gameFeeS = [formatter stringFromNumber: gameN];
csgoFeesDisplay.text = [NSString stringWithString: gameFeeS];
steamFeeS = [formatter stringFromNumber: steamN];
steamFeesDisplay.text = [NSString stringWithString: steamFeeS];
答案 0 :(得分:0)
您需要执行类似
的操作$(function() {
$(".advertLink").on("click",function(e) {
e.preventDefault(); // stop the link unless it has a target _blank or similar
var href = this.href;
var id=this.id; // or $(this).data("advertid") if you have data-advertid="advert1" on the link
$.post("./wp-content/plugins/facilitaire-advert-widget/save-advert-click.php",
{ "action":"call_this", "advert_ID":id },
function() {
location=href; // or window.open
}
);
});
});
使用
<a href="url_to_load" id="advert1" class="advertLink">Link of Advert</a>