如何在PHP中将一个PHP变量插入到JavaScript中的代码部分?

huangapple go评论110阅读模式
英文:

How do I insert a php variable inside javascript inside PHP

问题

  1. 我在本地网络服务器上使用了以下代码没有任何问题
  2. ```php
  3. function redirect($url) {
  4. header('Location: '.$url);
  5. exit();
  6. }

但在我的托管服务器上出现了“输出已发送”的错误。我设计了一个JavaScript 解决方法,如下所示:

  1. function redirect_to($new_location)
  2. {
  3. $redirects = [
  4. "<script>window.location.replace(\"login.php\");</script>",
  5. "<script>window.location.replace(\"select_set.php\");</script>",
  6. "<script>window.location.replace(\"run_order.php\");</script>",
  7. "<script>window.location.replace(\"display_setlist.php\");</script>",
  8. "<script>window.location.replace(\"manage_admins.php\");</script>",
  9. "<script>window.location.replace(\"clients.php\");</script>",
  10. "<script>window.location.replace(\"manage_songs.php\");</script>",
  11. "<script>window.location.replace(\"admin.php\");</script>"
  12. ];
  13. for ($i=0;$i<count($redirects);$i++) {
  14. if (strpos($redirects[$i], $new_location)) {
  15. echo $redirects[$i];
  16. }
  17. }
  18. }

这种方法不够优雅,每次重定向都需要编写一个单独的脚本。

我的问题是:是否有办法将 PHP 变量 $new_location 插入到 JavaScript 字符串中?是否可行?

我学会了如何在 PHP 和 JavaScript 中相互插入代码。我尝试过以各种方式结合这两种插入方法,但没有成功。

  1. <details>
  2. <summary>英文:</summary>
  3. I used:
  4. `function redirect($url) {header(&#39;Location: &#39;.$url);
  5. exit();
  6. }`
  7. on my local network server without any problems but it failed with &#39;output already sent&#39; errors on my hosting server. I devised a javascript work-around that works as shown in the code below.

function redirect_to($new_location)
{
$redirects = [
&quot;&lt;script&gt;window.location.replace(\&quot;login.php\&quot;);&lt;/script&gt;&quot;,
&quot;&lt;script&gt;window.location.replace(\&quot;select_set.php\&quot;);&lt;/script&gt;&quot;,
&quot;&lt;script&gt;window.location.replace(\&quot;run_order.php\&quot;);&lt;/script&gt;&quot;,
&quot;&lt;script&gt;window.location.replace(\&quot;display_setlist.php\&quot;);&lt;/script&gt;&quot;,
&quot;&lt;script&gt;window.location.replace(\&quot;manage_admins.php\&quot;);&lt;/script&gt;&quot;,
&quot;&lt;script&gt;window.location.replace(\&quot;clients.php\&quot;);&lt;/script&gt;&quot;,
&quot;&lt;script&gt;window.location.replace(\&quot;manage_songs.php\&quot;);&lt;/script&gt;&quot;,
&quot;&lt;script&gt;window.location.replace(\&quot;admin.php\&quot;);&lt;/script&gt;&quot;
];
for ($i=0;$i&lt;count($redirects);$i++) {
if (strpos($redirects[$i], $new_location)) {
echo $redirects[$i];
}
}
}

  1. It&#39;s not elegant and requires a separate script to be written for each redirect.
  2. My question: Is there a way to insert the PHP variable &#39;$new_location&#39; into the javascript string. Can it be done?
  3. I learned how to insert javascript into php and vice versa. I&#39;ve tried combining the two insertion methods in various ways without success.
  4. </details>
  5. # 答案1
  6. **得分**: -1
  7. 你真的应该使用 .htaccess 文件

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=301]

  1. 查找和头部重定向应该可以工作,你应该调查为什么它不起作用。
  2. 你很可能插入了空格,如

?>

<?php

  1. 一旦弄清楚托管情况,你可以使用以下版本。他们的帮助台应该能够提供帮助。
  2. ```php
  3. function redirect_to($new_location) {
  4. $redirects = [
  5. 'login' => 'login.php',
  6. 'select_set' => 'select_set.php',
  7. 'run_order' => 'run_order.php',
  8. 'display_setlist' => 'display_setlist.php',
  9. 'manage_admins' => 'manage_admins.php',
  10. 'clients' => 'clients.php',
  11. 'manage_songs' => 'manage_songs.php',
  12. 'admin' => 'admin.php'
  13. ];
  14. if(array_key_exists($new_location, $redirects)) {
  15. header('Location: ' . $redirects[$new_location]);
  16. exit;
  17. }
  18. }
英文:

You really should use .htaccess

  1. RewriteEngine on
  2. RewriteCond %{REQUEST_FILENAME}.php -f
  3. RewriteRule ^(.+?)/?$ /$1.php [L,R=301]

A lookup and header redirect should work and you should investigate why it does not work.

You likely inserted whitespace like

  1. ?&gt;
  2. &lt;?php

Here is a version you can use once you figured out what is going on on the hosting. Their helpdesk should be able to help.

  1. function redirect_to($new_location) {
  2. $redirects = [
  3. &#39;login&#39; =&gt; &#39;login.php&#39;,
  4. &#39;select_set&#39; =&gt; &#39;select_set.php&#39;,
  5. &#39;run_order&#39; =&gt; &#39;run_order.php&#39;,
  6. &#39;display_setlist&#39; =&gt; &#39;display_setlist.php&#39;,
  7. &#39;manage_admins&#39; =&gt; &#39;manage_admins.php&#39;,
  8. &#39;clients&#39; =&gt; &#39;clients.php&#39;,
  9. &#39;manage_songs&#39; =&gt; &#39;manage_songs.php&#39;,
  10. &#39;admin&#39; =&gt; &#39;admin.php&#39;
  11. ];
  12. if(array_key_exists($new_location, $redirects)) {
  13. header(&#39;Location: &#39; . $redirects[$new_location]);
  14. exit;
  15. }
  16. }

huangapple
  • 本文由 发表于 2023年6月22日 14:03:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76528971.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定