用于将变量添加到会话和数据库的按钮

发布于 2024-11-14 13:50:06 字数 4105 浏览 1 评论 0原文

好吧,我对这一切都很陌生,所以这可能看起来是一个冗长的方法。我想做的是对于一个按钮,它只是将变量(这是一个 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 .= "&nbsp;&nbsp;<input type=\"image\" src=\"images/favorites_hover.gif\" name=\"favorites_remove\" value=\"submit\" title=\"Remove from Favorites\"/>";
                }else{
                    $quote .= "&nbsp;&nbsp;<input type=\"image\" src=\"images/favorites_add.gif\" name=\"favorites_submit\" value=\"submit\" title=\"Add to Favorites\"/>";
                }
$quote .= "</form>";
?>

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($_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>";
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

来世叙缘 2024-11-21 13:50:06

当使用图像作为提交按钮时,浏览器通常会发送您单击图像的位置,而不是发送图像是否被单击。浏览器不会发送 chalkboard_submit=submit,而是发送 chalkboard_submit_x=20chalkboard_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' 是否都已设置,因为如果其中一个设置,则另一个也必须设置。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文