好的,我是所有这一切的新手,所以这似乎是一个冗长的方法。我试图做的是一个按钮,它只是将变量(这是一个id)添加到会话。这是一个临时列表,用户可以在站点中访问该列表。我通过POST提交这样做。这个特殊的按钮我称之为黑板。另一个按钮是收藏夹。用户可以单击并将id添加到他们的收藏夹(存储在数据库中),这样当他们返回并登录时,收藏夹仍然存在。同样,这是使用POST提交。我对我的态度似乎不稳定,并且在最新版本的Firefox(4.01)中突然停止工作。理想情况下,我会有一个不提交的按钮(导致页面重新加载),但是有一个在onclick上更改的实时按钮,但在后台执行所有操作
/////Chalkboard///// (adding/removing to session)
<?php
if(isset($_POST['chalkboard_submit'])){
$_SESSION['chalkboard'][] = $_POST['cb'];
}
elseif(isset($_POST['chalkboard_remove'])){
$_SESSION['chalkboard'] = array_diff($_SESSION['chalkboard'], array($_POST['cb']));
}
/////Favorites///// (adding/removing to database)
if(isset($_POST['favorites_submit'])){
$fav_query = "SELECT favorites FROM users
WHERE id = {$_SESSION['id']}";
$fav_result = mysql_query($fav_query,$connection);
$row = mysql_fetch_array($fav_result);
if(empty($row['favorites'])){
$favorites = array();
$favorites[] = $_POST['fav'];
$_SESSION['favorites'] = $favorites;
$favorites = __serialize($favorites);
}elseif(!empty($row['favorites'])){
$favorites = __unserialize($row['favorites']);
if(!in_array(($_POST['fav']), $favorites)){
$favorites[] = $_POST['fav'];}
$_SESSION['favorites'] = $favorites;
$favorites = __serialize($favorites);
}
$fav_insert = "UPDATE users SET
favorites = '{$favorites}'
WHERE id = '{$_SESSION['id']}'
LIMIT 1
";
$fav_result = mysql_query($fav_insert,$connection);
}
elseif(isset($_POST['favorites_remove'])){
$fav_query = "SELECT favorites FROM users
WHERE id = {$_SESSION['id']}";
$fav_result = mysql_query($fav_query,$connection);
$fav_row = mysql_fetch_array($fav_result);
$favorites = __unserialize($fav_row['favorites']);
$favorites = array_diff($_SESSION['favorites'], array($_POST['fav']));
$_SESSION['favorites'] = $favorites;
$favorites = __serialize($favorites);
$fav_insert = "UPDATE users SET
favorites = '{$favorites}'
WHERE id = '{$_SESSION['id']}'
LIMIT 1
";
$fav_result = mysql_query($fav_insert,$connection);
}
?>
///////// FORM ///////////
<?php
$quote = "<a name=\"" . $quotes['id'] . "\"></a>
<form action=\"favorites.php?subj=" . $_SESSION['subj'] . "#" . $quotes['id'] . "\" method=\"post\">
<a href=\"quote_details.php?id=" . $quotes['id'] . "\">\"" . $quotes['quote'] . "\"</a>
<input type=\"hidden\" value=\"" . $quotes['id'] . "\" name=\"cb\" />
<input type=\"hidden\" value=\"" . $quotes['id'] . "\" name=\"fav\" />";
###################### CHALK-BOARD #################################
if(isset($_SESSION['chalkboard']) && in_array($quotes['id'], $_SESSION['chalkboard'])){
$quote .= "<input type=\"image\" src=\"images/chalk_board_add_active.gif\" name=\"chalkboard_remove\" value=\"submit\" title=\"Remove from Chalk-board\"/>";
}else{
$quote .= "<input type=\"image\" src=\"images/chalkboard_add.gif\" name=\"chalkboard_submit\" value=\"submit\" title=\"Add to Chalk-board\"/>";
}
####################### FAVORITES ################################
if(isset($_SESSION['favorites']) && in_array($quotes['id'], $_SESSION['favorites'])){
$quote .= " <input type=\"image\" src=\"images/favorites_hover.gif\" name=\"favorites_remove\" value=\"submit\" title=\"Remove from Favorites\"/>";
}else{
$quote .= " <input type=\"image\" src=\"images/favorites_add.gif\" name=\"favorites_submit\" value=\"submit\" title=\"Add to Favorites\"/>";
}
$quote .= "</form>";
?>
答案 0 :(得分:0)
当使用图像作为提交按钮时,浏览器通常会发送您单击图像的位置,而不是单击它。浏览器不会发送chalkboard_submit=submit
,而是发送chalkboard_submit_x=20
和chalkboard_submit_y=15
(这些数字会因用户点击图片的位置而异。)
某些浏览器可能仍然只发送chalkboard_submit=submit
,而其他浏览器会发送所有这三种浏览器。要解决此问题并保持与大多数浏览器的兼容性,您需要检查两种方式。变化:
if(isset($_POST['chalkboard_submit'])){
$_SESSION['chalkboard'][] = $_POST['cb'];
}
elseif(isset($_POST['chalkboard_remove'])){
$_SESSION['chalkboard'] = array_diff($_SESSION['chalkboard'], array($_POST['cb']));
}
if(isset($_POST['favorites_submit'])){
elseif(isset($_POST['favorites_remove'])){
为:
if(isset($_POST['chalkboard_submit']) || isset($_POST['chalkboard_submit_x'])){
$_SESSION['chalkboard'][] = $_POST['cb'];
}
elseif(isset($_POST['chalkboard_remove']) || isset($_POST['chalkboard_remove_x'])){
$_SESSION['chalkboard'] = array_diff($_SESSION['chalkboard'], array($_POST['cb']));
}
if(isset($_POST['favorites_submit']) || isset($_POST['favorites_submit_x'])){
elseif(isset($_POST['favorites_remove']) || isset($_POST['favorites_remove_x'])){
它应该工作。
您无需检查_x'
和_y'
是否已设置,因为如果是另一个则必须也是。