Ok, Im new to all of this, so this may seem like a long-winded approach. What Im trying to do is for one button, it simply adds the variable (which is an id) to a session. This is for a temporary list that the user can access while in the site. Im doing this by a POST submit. This particular button I call the chalkboard. The other button is the favorites. The user can click and add the id to their favorites (stored in a database) so that when they return and log in, the favorites are still there. Again, this is using a POST submit. My approach to me seems choppy, and in the latest version of Firefox (4.01) has suddenly stopped working. Ideally, I would have a button that doesn't submit (which causes the page to reload) but to have a live button that changes on the onclick but does everything in the background
/////Chalkboard///// (adding/removing to session)
<?php
if(isset($_POST['chalkboard_submit'])){
$_SESSION['chalkboard'][] = $_POST['cb'];
}
elseif(isset(开发者_StackOverflow中文版$_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>";
?>
When using images as submit buttons, the browser will usually send where you clicked the image, instead of if it was clicked. The browser, instead of sending chalkboard_submit=submit
, will send chalkboard_submit_x=20
and chalkboard_submit_y=15
(those numbers will vary depending on where the user clicked on the image).
Some browsers may only still send chalkboard_submit=submit
, and others will send all three. To fix this and keep compatibility with most browsers you'll need to check both ways. Change:
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'])){
to:
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'])){
and it should work.
You don't need to check if both _x'
and _y'
isset because if one is the other has to be too.
精彩评论