【问题标题】:change PHP variable with onclick and echo the variable使用 onclick 更改 PHP 变量并回显该变量
【发布时间】:2014-04-28 20:19:26
【问题描述】:

我想在单击特定 div 时更改 PHP 变量的值。
我可以看到这是可能的唯一方法是通过某种方式刷新网页,因此它可以再次读取变量,但我不需要刷新整个页面,我只需要刷新其中的一些。
onclick 也需要执行两件事。

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/include/functions.php";
include($path);
randomopponent();
$chosenopponent = "Choose a opponent";
?>
<html>
  <head>
    <link rel="stylesheet" href="include/css/logged_in.css" />
    <title> testsite</title>
  </head>
  <body>
    this is a protected page! <br>
    <a href="#chooseopponent">chooseopponent</a>
    <div id="chooseopponent" class="chooseopponent"> <!-- This is the part that chosses the opponent -->
      <div>
        <h2>choose an opponent</h2>
        <table class="chooseopponenttable">
          <tr>
            <td class="chooseopponenttd" onclick='location.href = "#<?php echo "$opponent1"; ?>"'>
              <?php echo "$opponent1" . "<br>"; ?> <!-- defined in randomopponent(); -->
              <img
                class="chooseopponnetpic"
                src="/<?php echo $opponentpic1; ?>"
                alt="<?php echo "$opponent1"; ?>"> <!-- defined in randomopponent(); -->
            </td>
            <td class="chooseopponenttd" onclick='location.href = "#<?php echo "$opponent2"; ?>"'>
              <?php echo "$opponent2" . "<br>"; ?> <!-- defined in randomopponent(); -->
              <img
                class="chooseopponnetpic"
                src="/<?php echo $opponentpic2; ?>"
                alt="<?php echo "$opponent2"; ?>"> <!-- defined in randomopponent(); -->
            </td>
            <td class="chooseopponenttd" onclick='location.href = "#<?php echo "$opponent3"; ?>"'> 
              <?php echo "$opponent3" . "<br>"; ?> <!-- defined in randomopponent(); -->
              <img
                class="chooseopponnetpic"
                src="/<?php echo $opponentpic3; ?>"
                alt="<?php echo "$opponent3"; ?>" > <!-- defined in randomopponent(); -->
            </td>
          </tr>
        </table>
      </div>
    </div>
    <?php
    echo "<br>";
    echo $chosenopponent;
    ?>
    <div class="logoutbox"> <!-- Logout button -->
      <input
        type="button"
        onclick="location.href = 'index.php?logout';"
        Value="Logout"
        class="logout_button"
        />
    </div>

  </body>
</html>

所以我不知道该怎么做的部分是,例如当单击 $opponent3 时,$chosenopponent 现在应该是 $chosenopponent = $opponent3 并回显。

【问题讨论】:

  • 你误解了php的工作原理;它是一个服务器端脚本,一旦脚本完成并呈现页面,所有变量都会丢失。如果您需要存储值,您应该查看会话或数据库。或者您可以使用 javascript - 即客户端 - 并且仅在用户提交表单或类似内容时保存您的值。
  • 嗯,好吧,我的理解是,我必须将我所有的 php 值存储在 javascript 中,以便在用户与页面进行一些交互后能够使用它们。
  • 或者当有人在浏览器中更改某些内容时,使用 ajax 将它们直接存储在服务器端。
  • 嗯,你知道一个好的指南或类似的东西,它解释了如何使用 Ajax 做到这一点。 (我对 ajax 没有任何经验)

标签: javascript php jquery html


【解决方案1】:

您是否研究过 jQuery .load() 函数?您可以将它与 $_GET 结合使用,但您需要调整您的 html 以包含:

<div class='coppdiv'>

</div>

例如当对手被点击时:

$(".coppdiv").load("get_opponent.php?opp="+ opp_id);

【讨论】:

  • 不,我没有,我不知道那个功能。会检查的
猜你喜欢
  • 2016-03-20
  • 1970-01-01
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
  • 2011-10-16
  • 1970-01-01
  • 2021-07-31
  • 2016-02-24
相关资源
最近更新 更多