简体   繁体   English

PHP:PHP代码里面Javascript的使用方法

[英]PHP: How to use Javascript inside PHP code

I would like to run one of JS scripts while redirecting to another webpage.. Script to change a variable in Index.php like below code:我想在重定向到另一个网页时运行一个 JS 脚本。更改 Index.php 中变量的脚本如下代码所示:

if ($flag == 1) {
    // echo "<h1> Welcome to Website Home Page </h1>";
    if(header("Location: ../../index2.php")) {
        echo "<script type='text/javascript'> document.getElementById(body).style.color = 'red'; </script>";

    };

} else {

    // echo "<h1>Try Again!!!</h1>";
    echo "<div class='alert alert-danger' role='alert'>
    Your are not a Registered User <i class='fas fa-exclamation-triangle'></i> <br> Please Use below registrarion form.</div>";

    }

NB: Still Junior learning PHP. NB: Still Junior learning PHP.

An HTTP redirect tells the browser that it should get the data it asked for from a different URL. HTTP 重定向告诉浏览器它应该从不同的 URL 获取它请求的数据。

The body of a redirect response should be a message telling the user that they need to request a different URL. It is used in clients which either don't have HTTP redirect support or which have it disabled.重定向响应的主体应该是一条消息,告诉用户他们需要请求一个不同的 URL。它用于不支持 HTTP 重定向支持或禁用它的客户端。 Today such clients are practically non-existent.今天这样的客户几乎不存在。

If you want to run JS on the page being redirected to then you need to embed that JS in that page .如果你想在被重定向到的页面上运行 JS,那么你需要将该 JS 嵌入到该页面中

The header function not is for check URL, this function for send HTTP header. header function 不是查询 URL,这个 function 是发送 HTTP header。

For check url path using REQUEST_URI in superglobal variable $_SERVER.使用超全局变量 $_SERVER 中的 REQUEST_URI 检查 url 路径。

REQUEST_URI - the URI which was given in order to access this page; REQUEST_URI - 为访问此页面而提供的 URI; for instance, '/index.html'.例如,“/index.html”。

More: https://www.php.net/manual/en/reserved.variables.php更多: https://www.php.net/manual/en/reserved.variables.php

Run the following code for research:运行以下代码进行研究:

echo '<pre>';
var_dump($_SERVER)

About your JS code: You forgot to enclose the body in quotation marks关于您的 JS 代码:您忘记将主体用引号引起来

I think it will help you.我认为它会对你有所帮助。

Add a slash before all single quote ' -- > \'在所有单引号之前添加斜线 ' -- > \'

But above code dose not look ok...但是上面的代码看起来不太好......

You should add an event Listener, onclick, on something(when you check the URL) and then include your js in a function.您应该在某物上添加一个事件侦听器 onclick(当您检查 URL 时),然后将您的 js 包含在 function 中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM