在PHP中调用JavaScript变量的值可以通过以下几种方式实现:
前端代码:
var jsVariable = 'Hello World';
$.post('backend.php', { variable: jsVariable });
后端代码(backend.php):
$jsVariable = $_POST['variable'];
echo $jsVariable;
$_POST
或$_GET
获取该值。前端代码:
<input type="hidden" id="hiddenVariable" name="hiddenVariable" value="Hello World">
<form action="backend.php" method="post">
<input type="submit" value="Submit">
</form>
后端代码(backend.php):
$jsVariable = $_POST['hiddenVariable'];
echo $jsVariable;
$_GET
获取该值。前端代码:
var jsVariable = 'Hello World';
window.location.href = 'backend.php?variable=' + jsVariable;
后端代码(backend.php):
$jsVariable = $_GET['variable'];
echo $jsVariable;