gpt4 book ai didi

javascript - PHP URL 变量转换为 Javascript

转载 作者:行者123 更新时间:2023-12-03 08:23:34 24 4
gpt4 key购买 nike

在发现为什么我的网站不通过 header 命令重定向后,我发现了另一个问题。问题是我想将一些变量放入 url 重定向中,即在 javascript 中。这是我的代码的一部分:

<?php
$cost = 200
$name = 'Bill'
$url_endpoint = 'https://example.com/item1='.$cost.'&n='.$name.'.html';
if( !headers_sent() ) {
header("Location: $url_endpoint");
}
else {
?>

<script type="text/javascript">
document.location.href="$url_endpoint"; //That's just example what I want
</script>
Redirecting to <a href="$url_endpoint">site.</a> //Same here

<?php
}
die();
exit;
} // ?
?>

对于初学者有什么想法/技巧吗?

最佳答案

您的脚本只需要对 Javascript 中的变量输出进行轻微修改。

你只需要记住,当你使用?>关闭PHP时,PHP 变量不再可访问,直到您使用 <? 再次打开 PHP。

您还使用了过多的右括号,这可能最终导致 PHP 错误。

<?php
$cost = 200
$name = 'Bill'
$url_endpoint = 'https://example.com/item1='.$cost.'&n='.$name.'.html';
if( !headers_sent() ){
header("Location: $url_endpoint");
}else{
?>
<script type="text/javascript">
document.location.href="<?php echo $url_endpoint;?>";
</script>
Redirecting to <a href="<?php echo $url_endpoint;?>">site.</a>
<?php
die();
exit;
}
?>

关于javascript - PHP URL 变量转换为 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33649234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com