在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;