【问题标题】:Score board assignment记分牌分配
【发布时间】:2015-10-24 19:02:45
【问题描述】:

我需要一个可以在您每次按 Verhogen 时计算得分的应用程序。我尝试了会话,但显然这两个会话相互冲突。有没有办法解决这个问题?

<?php
session_start();

if (!isset($_POST['thuisverhogen'])) {
    $_SESSION['thuisverhogen'] = 0;
} elseif (isset($_POST['thuisverhogen'])) {
    if (!($_SESSION['thuisverhogen'])) {
        $_SESSION['thuisverhogen'] = 1;
    } else {
        $thuisverhogen = $_SESSION['thuisverhogen'] + 1;
        $_SESSION['thuisverhogen'] = $thuisverhogen;
    }
}

if (!isset($_POST['uitverhogen'])) {
    $_SESSION['uitverhogen'] = 0;
} elseif (isset($_POST['uitverhogen'])) {
    if (!($_SESSION['uitverhogen'])) {
        $_SESSION['uitverhogen'] = 1;
    } else {
        $thuisverhogen = $_SESSION['uitverhogen'] + 1;
        $_SESSION['uitverhogen'] = $thuisverhogen;
    }
}
?>

<h2>SCORE</h2>
<form method="POST" action="2.php">
    Thuisscore: <input type="text" name="thuisscore" value="<?php echo $_SESSION['thuisverhogen'] ?>">
    <input type="submit" name="thuisverhogen" value="Verhogen"><br>

    Uitscore: <input type="text" name="uitscore" value="<?php echo $_SESSION['uitverhogen'] ?>">
    <input type="submit" name="uitverhogen" value="Verhogen">
</form>

【问题讨论】:

  • 您想在此浏览器会话中本地保存分数,还是稍后在另一个会话中访问它?
  • 就在浏览器本地。我知道我需要使用多个会话,但是如何使用。
  • 我会做的是用javascript而不是php来计算,还是你的应用程序需要php?
  • 你得到的输出是什么?或者至少,它现在在做什么?顺便问一下,你2.php的代码是什么?
  • 你的分数值的$_POST变量被命名为thuisscore和uitscore,但是你没有在你的php中使用这些值来添加。您使用按钮的值,这使得分数相互影响。您可以查看检查在哪个点使用哪些变量。

标签: php forms session post


【解决方案1】:

找到答案

if (isset($_POST['thuisscore'])) {

    $thuisscore = $_POST['thuisscore'];
} else {
    $thuisscore = 0;
}

if (isset($_POST['uitscore'])) {
    $uitscore = $_POST['uitscore'];
} else {
    $uitscore = 0;
}

//verhogen
if (isset($_POST['thuisverhogen'])) {
    $thuisscore = $thuisscore + 1;
} else {
    if (isset($_POST['verhooguit'])) {
        $uitscore = $uitscore + 1;
    }
}
?>

<h2>SCORE</h2>
<form method="POST" action="2.php">
    Thuisscore: <input type="text" name="thuisscore" value="<?php echo $thuisscore ?>">
    <input type="submit" name="thuisverhogen" value="Verhogen"><br>

    Uitscore: <input type="text" name="uitscore" value="<?php echo $uitscore ?>">
    <input type="submit" name="verhooguit" value="Verhogen"><br>

</form>

【讨论】:

    猜你喜欢
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多