我正在为一个网站开发一个“订阅模块”,除了
Ajax 部分,一切都很好。
当我第一次单击“订阅”时,按钮变为“取消订阅”,但如果我再次单击“取消订阅”按钮而不刷新页面,按钮将变为“订阅”,而不是再次“取消订阅”。
就像我点击了两次一样。
这是我从“chrome -> network tab”看到的
engine/ajax/mywatch.php?subscribe_id=3385&action=plus&skin=Default
engine/ajax/mywatch.php?subscribe_id=3385&action=minus&skin=Default
html
<a id="subscript-id-3385" onclick="doMywatch('3385', 'plus'); return false;" >subscribe</a>
javascript
<script>
function doMywatch(a,b){ShowLoading("");$.get(root+"engine/ajax/mywatch.php",{subscribe_id:a,action:b,skin:skin},function(b){HideLoading("");$("#subscript-id-"+a).html(b)});return!1}
</script>
php
if( $_GET['action'] == "plus" ) {
echo <<<HTML
<script>
alert('plus ');
</script>
HTML;
$row = $db->super_query("SELECT subscriptions FROM " . PREFIX . "_mywatch WHERE user_id={$member_id['user_id']}" );
$list = explode(",", $row['subscriptions']);
foreach ( $list as $daten ) {
if( $daten == $id ) $achecker = "stop";
}
$error = "";
if( $achecker != "stop" ){
$list[] = $id;
$subsc = $db->safesql(implode( ",", $list ));
//$subsc = implode( ",", $list );
if( $row['subscriptions'] == "" ) $subsc = $id;
$db->query( "UPDATE " . PREFIX . "_mywatch set subscriptions='$subsc' where user_id = '{$member_id['user_id']}'" );
}
$buffer = "<a onclick=\"doMywatch('" . $id . "', 'minus'); \" title=\"" . $lang['news_minfav'] . "\">unsubscribe</a>";
} elseif( $_GET['action'] == "minus" ) {
echo <<<HTML
<script>
alert('minus ');
</script>
HTML;
$error = "";
$row = $db->super_query("SELECT subscriptions FROM " . PREFIX . "_mywatch WHERE user_id={$member_id['user_id']}" );
$list = explode(",", $row['subscriptions']);
foreach ( $list as $daten ) {
if( $daten == $id ) $achecker = "stop";
}
if( $achecker == "stop" ){
$list = array_merge(array_diff($list, array($id)));
$subsc = $db->safesql(implode( ",", $list ));
//$subsc = implode( ",", $list );
$db->query( "UPDATE " . PREFIX . "_mywatch set subscriptions='$subsc' where user_id = '{$member_id['user_id']}'" );
}
$buffer = "<a onclick=\"doMywatch('" . $id . "', 'plus'); \" title=\"" . $lang['news_minfav'] . "\">Подписатся</a>";
} else
die( "error" );
$db->close();
@header( "Content-type: text/html; charset=" . $config['charset'] );
echo $buffer;