GVKun编程网logo

有没有办法从两个不同的 html 文件中使用 PHP?(如何将两个html文件联系起来)

3

对于想了解有没有办法从两个不同的html文件中使用PHP?的读者,本文将是一篇不可错过的文章,我们将详细介绍如何将两个html文件联系起来,并且为您提供关于Gitlab调度变量不能在yaml文件中使用

对于想了解有没有办法从两个不同的 html 文件中使用 PHP?的读者,本文将是一篇不可错过的文章,我们将详细介绍如何将两个html文件联系起来,并且为您提供关于Gitlab 调度变量不能在 yaml 文件中使用、Liquid::SyntaxError with Plotly 在 Markdown 文件中使用 Jekyll、ngx translate - 在 ts 文件中使用 get 或即时方法不起作用、Node.js 在多个 .js 文件中使用 socket.io的有价值信息。

本文目录一览:

有没有办法从两个不同的 html 文件中使用 PHP?(如何将两个html文件联系起来)

有没有办法从两个不同的 html 文件中使用 PHP?(如何将两个html文件联系起来)

如何解决有没有办法从两个不同的 html 文件中使用 PHP?

我正在尝试单独使用 PHP 制作一个用户通行证系统,使用数组而不是使用 sql(我不知道这是否是正确的方法)。我正在定义来自 2 个不同 html 文件的变量,如下所示:

  1. $user = $_POST[''user'']; // From signup.html
  2. $pass = $_POST[''pass'']; // From signup.html
  3. $email = $_POST[''email'']; // From signup.html
  4. $user2 = $_POST[''userlog'']; // From login.html
  5. $pass2 = $_POST[''passlog'']; // From login.html
  6. ...

使用这些变量(点-点-点)的代码没有做任何事情,就像我一开始甚至没有声明它们一样。我之前看过 include 和 require ,但我不知道如何使用它们,所以我认为这可能是我必须以某种方式做的事情。

解决方法

你的问题含糊不清,所以我会尽量给你一个开始的一般提示。您将需要从那里开始工作到您真正想要实现的目标:

您当然可以从各种其他文件中引用单个 php 脚本,也可以从 html 文件中引用,因为 php 可以嵌入到 html 中。您所要做的就是将该脚本包含在您的 html 文件中:

  1. <html>
  2. <head>
  3. ...
  4. </head>
  5. <body>
  6. ...
  7. <?php include path/to/script.php" ?>
  8. <h1>Some title</h1>
  9. <?php if ($user = "alf") then { ?>
  10. <h2>Hello Alf!</h2>
  11. <?php } else { ?>
  12. <h2>Sorry,I do not know you...</h2>
  13. <?php } ?>
  14. ...
  15. </body>
  16. </html>

然而,这并不是实现应用程序逻辑的典型方式。

通常的做法是:

  1. 不要使用 html 文件,而只使用 php 脚本。那些可以包含或输出 html 标记,没有问题,但编码更容易。
  2. 不要试图重新发明轮子,有许多现成的用户管理解决方案。不要再犯别人已经犯过的所有错误。
  3. 使用数据库,如果你不想使用 mysql 那么也许 sqlite3 是一个替代方案,它更容易设置,有时也足够了,尽管它肯定比 mysql 服务器慢得多。
,

假设您的表单重定向到一个 PHP 文件,您可以使用它:

  1. $user = ((isset($_POST[''user'']))? $_POST[''user''] : ((isset($_POST[''user2'']))? $_POST[''user2'']) : ''''));
  2. $pass = ((isset($_POST[''pass'']))? $_POST[''pass''] : ((isset($_POST[''passlog'']))? $_POST[''passlog''] : ''''));
  3. $email = ((isset($_POST[''email'']))? $_POST[''email''] : '''');

它使用 isset(),更多信息在这里:

https://www.php.net/manual/en/function.isset.php

Gitlab 调度变量不能在 yaml 文件中使用

Gitlab 调度变量不能在 yaml 文件中使用

如何解决Gitlab 调度变量不能在 yaml 文件中使用

我一直在尝试在 gitlab 上运行我的自动化脚本。 下面是我的 yaml 文件。

  1. build_job_SelfNUNA:
  2. only:
  3. variables:
  4. - $type == "SelfNUNA"
  5. except:
  6. variables:
  7. - $suitetype == "regression"
  8. tags:
  9. - RunnerIPA
  10. script:
  11. - \\\\IPAPARTNERADMIN\\C$\\GitLab-Runner\\GitLab\\%VM1%.bat
  12. only:
  13. - master

在 CI/CD 中创建计划时,我使用变量键作为 VM1 和值作为 bat 文件的名称,但显示以下错误

  1. \\\\IPAPARTNERADMIN\\C$\\GitLab-Runner\\GitLab\\%VM1%.bat : The term ''\\\\IPAPARTNERADMIN\\C$\\GitLab-Runner\\GitLab\\%VM1%.bat''
  2. is not recognized as the name of a cmdlet,function,script file,or operable program. Check the spelling of the name,or if a path was included,verify that the path is correct and try again.
  3. At C:\\Windows\\TEMP\\build_script994464517\\script.ps1:207 char:1
  4. + \\\\IPAPARTNERADMIN\\C$\\GitLab-Runner\\GitLab\\%VM1%.bat
  5. + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. + CategoryInfo : ObjectNotFound: (\\\\IPAPARTNERADM...itLab\\%VM1%.bat:String) [],CommandNotFoundException
  7. + FullyQualifiedErrorId : CommandNotFoundException
  8. strong text

Liquid::SyntaxError with Plotly 在 Markdown 文件中使用 Jekyll

Liquid::SyntaxError with Plotly 在 Markdown 文件中使用 Jekyll

如何解决Liquid::SyntaxError with Plotly 在 Markdown 文件中使用 Jekyll

尝试将 .ipynb 笔记本转换为 Markdown,其中包含在 Jekyll 站点(托管在 GitHub Pages 上)中使用的 Plotly 图表。我遇到了这个错误:

Liquid Exception: Liquid Syntax error (line 134): Variable ''{{ x.observe(notebookContainer,{childList: true}'' was not properly terminated with regexp: /\\}\\}/ bundler: Failed to load command: jekyll

使用这个简单的笔记本来复制这个问题,链接 here。 Markdown 转换的文件是 here。

我尝试从 Jupyter 以转换格式(.md.html)下载笔记本,但遇到了同样的错误。也无法在 Xcode 中打开 Markdown 文件,因为当文件在我的项目目录中时程序在启动时冻结。

非常感谢您对此问题的任何帮助。

编辑:提到的 here 解决方法是删除 init_notebook_mode()pio.renderers.default = ''jupyterlab''。还要避免使用 pio.write_html() 直接在 notebook 中渲染 Plotly。导出到 .md,添加 YAML 前端,然后调用 {% include figure_name.html %}

解决方法

您发布的 JavaScript 文件似乎存在许多语法不一致的问题。提到的一个与 Liquid 模板的输出标签 {{ ... }} 发生冲突。

问题来了:

if (notebookContainer) {{
  x.observe(notebookContainer,{childList: true});
}}

不应该有双括号:

if (notebookContainer) {
  x.observe(notebookContainer,{childList: true});
}

ngx translate - 在 ts 文件中使用 get 或即时方法不起作用

ngx translate - 在 ts 文件中使用 get 或即时方法不起作用

如何解决ngx translate - 在 ts 文件中使用 get 或即时方法不起作用

我正在使用可导入其他模块的 sharedModule,因此我尝试在所有文件上使用 ngx-translate。 在 app.component.ts 中 get 方法运行良好,但在 test.component.ts 中不行。 奇怪的是,html 文件中的管道 ( | translate ) 运行良好。 就像线程在 app.component.ts 中执行 this.translateService.use(lang); 停止并转到 test.component.ts 的 this.translate.get(''testTranslation'') 一样,失败然后返回到 app.component.ts 并且它可以工作。 目前,我的代码如下所示:

我的结构:

-> src -> app.module.ts
       -> app.component.ts

       -> shared/shared.module.ts

       -> test/test.module.ts
              /test.component.ts

app.module.ts

import { TranslateModule,TranslateLoader } from ''@ngx-translate/core'';
import { TranslateHttpLoader } from ''@ngx-translate/http-loader'';
import { SharedModule } from ''../shared/shared.module'';
import { TestModule } from ''../test/test.module'';

export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http,''../../assets/i18n/'',''.json'');
}

@NgModule({
declarations: [ *(some component)* ],imports: [ 
    *(other modules)*,SharedModule,TestModule,TranslateModule.forRoot({
        loader: {
            provide: TranslateLoader,useFactory: (createTranslateLoader),deps: [HttpClient]
        },}) 
],})
export class AppModule { }

app.component.ts

import { TranslateService } from ''@ngx-translate/core'';

constructor() {
    const lang = window.navigator.language;
    try {
      this.translateService.setDefaultLang(lang);
      this.translateService.use(lang);
      this.translate.get(''testTranslation'').subscribe((res: string[]) => { // instant doesn''t work too
      console.log(res); // expected: ''Translation Works''
                        // result: ''Translation Works''
    } catch (e) {
        console.log(''Language file is not found: '' + lang,e);
    }
}

shared.module.ts

import { TranslateModule,TranslateLoader } from ''@ngx-translate/core'';
import { TranslateHttpLoader } from ''@ngx-translate/http-loader'';

export function createTranslateLoader(http: HttpClient) {
  return new TranslateHttpLoader(http,TranslateModule.forChild({
        loader: {
            provide: TranslateLoader,exports: [
    TranslateModule
],})
export class SharedModule { }

test.module.ts

import { SharedModule } from ''../shared/shared.module'';

@NgModule({
declarations: [ 
    *(some component)* 
],],})
export class SharedModule { }

test.component.ts

  import { TranslateService } from ''@ngx-translate/core'';

  constructor(private translate: TranslateService) {
  }

  ngOnInit(): void {
    this.translate.get(''testTranslation'').subscribe((res: string[]) => { // instant doesn''t work too
      console.log(res); // expected: ''Translation Works''
                        // result: ''testTranslation''
    });

我尝试了一些东西,但都没有奏效。

解决方法

在 app.module.ts 中尝试:

import {APP_INITIALIZER,Injector,NgModule} from ''@angular/core'';
import {LOCATION_INITIALIZED} from ''@angular/common'';
@NgModule({
    ...,providers: [
        ...,{
            provide: APP_INITIALIZER,useFactory: appInitializerFactory,deps: [TranslateService,Injector],multi: true
        }
    ]
})
export class AppModule {}

// tslint:disable-next-line:no-any
export  function appInitializerFactory(translateService: TranslateService,injector: Injector): () => Promise<any> {
  // tslint:disable-next-line:no-any
  return () => new Promise<any>((resolve: any) => {
    const locationInitialized = injector.get(LOCATION_INITIALIZED,Promise.resolve(null));
    locationInitialized.then(() => {
      translateService.use(window.navigator.language)
        .pipe(take(1))
        .subscribe(() => {},err => console.error(err),() => resolve(null));
    });
  });
}

Node.js 在多个 .js 文件中使用 socket.io

Node.js 在多个 .js 文件中使用 socket.io

如何解决Node.js 在多个 .js 文件中使用 socket.io?

我希望能够在多个 .js 文件中编写函数并使用在我的主 server.js 中创建的相同套接字

server.js:


var express = require(''express'')
var app = express()
var http = require(''http'')
var server = http.createServer(app)
var io = require(''socket.io'')(server)
var GOR = require(''./gor'')
var path = require(''path'');
app.use(express.static(path.join(__dirname,''/public'')));

//handles get request and serves index.html
app.get(''/'',function(req,res) {
    res.sendFile(__dirname + ''/public/index.html'');
});

//wait for server to start
server.listen(8080,()=>{
  console.log("server started");
  setInterval(emit_data,5000);
  emit_data();
  
  //setInterval(GOR.send_from_another_js,5000);
  //GOR.send_from_another_js(io);
})

function emit_data(){
    io.emit( ''update'',"webserver.js");
}

从上面的代码可以看出,我有一个函数每 5 秒向套接字发送一次,并且工作正常。我已经确认过了。

我现在要做的就是创建一个 seperate.js 文件并将我的所有函数保留在那里,以使代码看起来更清晰。我将另一个 .js 文件命名为 gor.js:

gor.js:


//I want to call this function in my webserver.js and transmit this socket
function send_from_another_js(io){
    console.log("function called");
    io.emit( ''update'',"another_js");
}

module.exports = {
    send_from_another_js    
}

当我在 server.js 中调用这个函数时,它不起作用:

server.listen(8080,()=>{
  console.log("server started");
  setInterval(GOR.send_from_another_js,5000);
  GOR.send_from_another_js(io);

})

在其他 .js 文件中使用相同 .io 的正确方法是什么?以上无效。

EDIT1

在我的 .html 中,我等待套接字上的消息:

window.addEventListener(''DOMContentLoaded'',() => {
    
    socket.on(''update'',function(number_to_set) {
    console.log("socket update received");
    document.getElementById(''number1'').innerHTML = number_to_set;

});
    
    var button = document.getElementById("operation_code_button");
    button.addEventListener("click",function(event){
    var val = document.getElementById("operation_code_input").value;
    console.log("socket clicked,emmiting data");
    socket.emit(''button_click'',val);
    
});

})

在我的 .js 文件中,我每 5 秒向这个套接字发送一次:

server.listen(8080,()=>{
    console.log("server started");
    setInterval(emit_data,5000);
    emit_data();
})

5 秒后,我可以看到我的网页更新数据,一切正常!!!

我想在另一个 .js 文件中声明 emit_data 函数并在我的主 .js 文件中使用它。

我的辅助 .js 文件 (gor.js) 中的函数:

function send_from_another_js_1(io){
  io.emit( ''update'',data);
}

我想在我的主 .js 中调用它,就像调用 emit_data 一样

解决方法

已编辑:(3) 相信这就是您正在寻找的...

“server.js”

const content = require(''fs'').readFileSync(__dirname + ''/index.html'',''utf8'');
const httpServer = require(''http'').createServer((req,res) => {
// serve the index.html file
res.setHeader(''Content-Type'',''text/html'');
res.setHeader(''Content-Length'',Buffer.byteLength(content));
res.end(content);
});

const io = require(''socket.io'')(httpServer);

const apple = require("./File2.js")
apple.send_from_another_js_1(io);
apple.send_from_another_js_2(io);

var port = 3000; //wamp server
var prompt = ''Open browser at http://localhost:''
httpServer.listen(port,() => {
console.log(prompt,port);
});

“File2.js”

function send_from_another_js_1(io){
  io.on(''connection'',function (socket) {
  socket.on(''file2_1_EventTrigged'',function (data) {
  socket.emit(''update1'',"send_from_another_js_1(io) : "+ data);
  console.log("+++ function send_from_another_js_1(io) called");
  });
 });
}

function send_from_another_js_2(io){
  io.on(''connection'',function (socket) {
  socket.on(''file2_2_EventTrigged'',function (data) {
  socket.emit(''update2'',"send_from_another_js_2(io) : "+ data);
  console.log("... function send_from_another_js_2(io) called");
  });
 });
}

module.exports = {
 send_from_another_js_1,send_from_another_js_2
}

"index.html"

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="Content-Type" content="text/html">
 <meta name="viewport" content="width=device-width,initial-scale=1">
 <meta name="keywords" content="HTML,CSS,JavaScript">
 <meta name="author" content="Dr.Chaiya Tantisukarom">
 <meta http-equiv="Cache-Control" content="public,max-age=31536000">
 <title>NodeJS socket.io</title>
</head>
<body>
 <h1>Hello another world</h1>
 <div>
  <span>
   <button onclick="file2_1()">file2_1</button>
  </span>
  <span>
   <button onclick="file2_2()">file2_2</button>
  </span>
 </div>

<script src="/socket.io/socket.io.js"></script>
<script>
 var socket = io();
 socket.on(''connect'',()=>{
  //alert(''Connected'');
 })

function file2_1(){
 socket.emit(''file2_1_EventTrigged'',"Hello file2_1,how are you today?");
}
socket.on(''update1'',(data)=>{
 alert(data);
})

function file2_2(){
 socket.emit(''file2_2_EventTrigged'',"Hi file2_2,trust you are ok.");
}
socket.on(''update2'',(data)=>{
 alert(data);
})

</script>
</body>
</html>

干杯!!!

,

您需要等待服务器初始化。您正在 socket.io 准备好之前调用您的函数。这就是 io.emit 函数是“未定义”的原因。

server.listen(8080,()=>{
  GOR.send_from_another_js(io);
})

关于有没有办法从两个不同的 html 文件中使用 PHP?如何将两个html文件联系起来的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于Gitlab 调度变量不能在 yaml 文件中使用、Liquid::SyntaxError with Plotly 在 Markdown 文件中使用 Jekyll、ngx translate - 在 ts 文件中使用 get 或即时方法不起作用、Node.js 在多个 .js 文件中使用 socket.io等相关内容,可以在本站寻找。

本文标签: