用onclick更改PHP变量并回显变量

时间:2014-04-28 20:19:27

标签: javascript php jquery html html5

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

<?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并且回显出来。

1 个答案:

答案 0 :(得分:1)

你看过jQuery .load()函数吗?您可以将它与$ _GET结合使用,但您需要调整html以包含:

<div class='coppdiv'>

</div>

例如,当点击对手时:

$(&#34; .coppdiv&#34;)。load(&#34; get_opponent.php?opp =&#34; + opp_id);