我想在单击特定 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 并回显该内容。
最佳答案
您研究过 jQuery .load() 函数吗?您可以将其与 $_GET 结合使用,但您需要调整 html 以包含:
<div class='coppdiv'>
</div>
例如,当点击对手时:
$(".coppdiv").load("get_opponent.php?opp="+ opp_id);
关于javascript - 使用 onclick 更改 PHP 变量并回显该变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23350453/