0

I would like to change the value of a PHP variable when a specific div is clicked on.
The only way I can see this is possible is by somehow refresh the webpage, so it can read the variable again, But I do not need to refresh the whole page, i only need to refresh some of it.
Also the onclick needs to execute 2 things.

<?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>

SO the part I do not know how to do is when for example $opponent3 is clicked the $chosenopponent should now be $chosenopponent = $opponent3 and echo that out.

nazim
  • 1,439
  • 2
  • 16
  • 26
Oliver Nybroe
  • 1,828
  • 22
  • 30
  • 2
    You are misunderstanding how php works; it is a server-side script and as soon as the script finishes and the page renders, all variables are lost. If you need to store values, you should look into sessions or databases. Or you can use javascript - that is client-side - and only save your values when the user submits a form or something similar. – jeroen Apr 28 '14 at 20:21
  • http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor – Félix Adriyel Gagnon-Grenier Apr 28 '14 at 20:27
  • hmm okay, so they way i understand it is, that i have to store all my php values in a javascript, to be able to use them after the user have had some interaction with the page. – Oliver Nybroe Apr 28 '14 at 20:32
  • Or use ajax to store them directly server-side when somebody changes something in the browser. – jeroen Apr 28 '14 at 20:59
  • Hmm do you know a good guide or something like that, that explains how to do that with Ajax. (I do not have any experience with ajax) – Oliver Nybroe Apr 29 '14 at 09:17

1 Answers1

1

Have you looked into the jQuery .load() function? You can use it in conjunction $_GET, but you will need to adjust your html to contain:

<div class='coppdiv'>

</div>

For example, when the opponent is clicked:

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

Colum
  • 996
  • 2
  • 8
  • 23