我正在为网站制作一个“订阅模块”,一切正常,除了
Ajax部分。
当我点击“订阅”上的第一次时间时,按钮变为“取消订阅”,但如果我再次单击“取消订阅”按钮而不刷新页面,则按钮将变为“订阅”并且再次“取消订阅”。
就像我点击两次一样。
这是我从“chrome - >网络标签”
中看到的内容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;
答案 0 :(得分:1)
如果我是对的,那么当您第一次点击“susbcribe”时,您将进入elseif( $_GET['action'] == "plus" )
,然后您的输出将为<a onclick=......>Unsubscibe</a>
并将被放入您之前的<a id="subscript-id-3385">...</a>
内您将在同一元素中同时拥有订阅和取消订阅的点击事件,因此这可能会导致一些奇怪的行为。我建议你将你的链接包装在div中并仅更新该div。