表单提交后如何阻止变量重置?

发布于 2024-12-11 08:20:07 字数 1281 浏览 0 评论 0原文

我正在开发一个项目,我遇到了这个问题:我有一个名为 $category 的变量,如果为 null 或为空,则设置为 1。提交表单后,$category++,所以它变成 2。好的,但在第三次提交时,它并没有变成 3。我检查了发生的情况,发现实际上每次提交后,$category 都会变成 1,因为第一行代码表示如果它为 null 或空,它就会变成 1。让我展示一下你看代码就知道我在说什么!预先感谢您的帮助! :)

session_start();

include_once('db.php');

$cat_SQL = 'SELECT * FROM categories ORDER BY id';
$cat_RESULT = mysql_query($cat_SQL, $conn);
$cat_ROWS = mysql_fetch_assoc($cat_RESULT);
$cat_ROWS_number = mysql_num_rows($cat_RESULT);

if (is_null($category) || empty($category)) {
    $category = 1;
}

if (isset($category)) {
    if ($category < $cat_ROWS_number) {
        $category = $category + 1; echo $category;
    }
}

因此,如果我每次加载页面时都回显类别,则会得到如下结果:

第一次进入页面:类别为空,因此变为 1 => $类别 = 1 第一次提交后 $category = $category + 1 => $类别 = 2 第二次提交后 $category = $category + 1 => $category = 2(仍然是 2,而不是变成 3) 等等... :( 每次页面重新加载/提交时,如何阻止 $category 变为“1”,并变为新值(每个会话)?


所以我最终在看到你的答案之前成功地做到了(不是吹牛,而是很高兴我自己找到了答案:)))

if (!isset($_SESSION['category'])) {
  $_SESSION['category'] = 1;
  $category = $_SESSION['category'];
}
else {
      if($_SESSION['category'] < $cat_ROWS_number) {
          $_SESSION['category']++;
          $category = $_SESSION['category'];
      }
}

但是,谢谢大家的快速回复! :)

I'm working on a project and I'm stuck with this problem: I have a variable called $category, which if is null or empty is set to 1. After submitting the form, $category++, so it becomes 2. Okay, but at the third submit it doesn't become 3. I checked what happens and I found out that actually after each submit, the $category becomes 1, because of the first code line that says that if it's null or empty it becomes 1. Let me show you the code to see what I'm talking about! Thank you in advance for helping! :)

session_start();

include_once('db.php');

$cat_SQL = 'SELECT * FROM categories ORDER BY id';
$cat_RESULT = mysql_query($cat_SQL, $conn);
$cat_ROWS = mysql_fetch_assoc($cat_RESULT);
$cat_ROWS_number = mysql_num_rows($cat_RESULT);

if (is_null($category) || empty($category)) {
    $category = 1;
}

if (isset($category)) {
    if ($category < $cat_ROWS_number) {
        $category = $category + 1; echo $category;
    }
}

So if I echo the category each time the page loads, I have something like this:

First time you enter the page: category is empty so it becomes 1 => $category = 1
After 1st SUBMIT $category = $category + 1 => $category = 2
After 2nd SUBMIT $category = $category + 1 => $category = 2 (still 2 instead of becoming 3)
and so on... :(
HOW CAN I STOP $category from become '1' each time the page reloads / submits, and become the new value instead (per session)?


So I eventually managed to do it before seeing your answers (not bragging, but happy that I found the answer myself :)) )

if (!isset($_SESSION['category'])) {
  $_SESSION['category'] = 1;
  $category = $_SESSION['category'];
}
else {
      if($_SESSION['category'] < $cat_ROWS_number) {
          $_SESSION['category']++;
          $category = $_SESSION['category'];
      }
}

However, thank you all for your quick replies!!! :)

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

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

发布评论

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

评论(2

叶落知秋 2024-12-18 08:20:07

一种方法是将 $category 放入会话变量中。在 session_start 之后但在 if (is_null($category) ||empty($category)) 之前放置:

if(isset($_SESSION['category']))
    $category = $_SESSION['category'];

然后在 $category = $category 之后+ 1; 你输入:

$_SESSION['category'] = $category;

这应该可以解决问题。

One way to do this, is to put $category inside a session variable. After the session_start but before the if (is_null($category) || empty($category)) you put:

if(isset($_SESSION['category']))
    $category = $_SESSION['category'];

And then after the $category = $category + 1; you put:

$_SESSION['category'] = $category;

That ought to do the trick.

怎会甘心 2024-12-18 08:20:07

或者在表单中添加一个隐藏字段,在其中放置当前类别计数。

Or add a hidden field to your form where you put the current category count in.

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