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

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

How do I insert a php variable inside javascript inside PHP

问题

我在本地网络服务器上使用了以下代码没有任何问题

```php
function redirect($url) {
    header('Location: '.$url);
    exit();
}

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

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

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

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

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


<details>
<summary>英文:</summary>

I used: 
    `function redirect($url) {header(&#39;Location: &#39;.$url);
         exit();
    }`
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];
}
}
}


It&#39;s not elegant and requires a separate script to be written for each redirect.

My question: Is there a way to insert the PHP variable &#39;$new_location&#39; into the javascript string. Can it be done?



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.

</details>


# 答案1
**得分**: -1

你真的应该使用 .htaccess 文件

RewriteEngine on

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


查找和头部重定向应该可以工作,你应该调查为什么它不起作用。

你很可能插入了空格,如

?>

<?php


一旦弄清楚托管情况,你可以使用以下版本。他们的帮助台应该能够提供帮助。

```php
function redirect_to($new_location) {
  $redirects = [
    'login' => 'login.php',
    'select_set' => 'select_set.php',
    'run_order' => 'run_order.php',
    'display_setlist' => 'display_setlist.php',
    'manage_admins' => 'manage_admins.php',
    'clients' => 'clients.php',
    'manage_songs' => 'manage_songs.php',
    'admin' => 'admin.php'
  ];

  if(array_key_exists($new_location, $redirects)) {
    header('Location: ' . $redirects[$new_location]);
    exit;
  }
}
英文:

You really should use .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.php -f
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

?&gt;

&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.

function redirect_to($new_location) {
  $redirects = [
    &#39;login&#39; =&gt; &#39;login.php&#39;,
    &#39;select_set&#39; =&gt; &#39;select_set.php&#39;,
    &#39;run_order&#39; =&gt; &#39;run_order.php&#39;,
    &#39;display_setlist&#39; =&gt; &#39;display_setlist.php&#39;,
    &#39;manage_admins&#39; =&gt; &#39;manage_admins.php&#39;,
    &#39;clients&#39; =&gt; &#39;clients.php&#39;,
    &#39;manage_songs&#39; =&gt; &#39;manage_songs.php&#39;,
    &#39;admin&#39; =&gt; &#39;admin.php&#39;
  ];

  if(array_key_exists($new_location, $redirects)) {
    header(&#39;Location: &#39; . $redirects[$new_location]);
    exit;
  }
}

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:

确定