简体   繁体   English

在 JavaScript 中调用 PhP 函数返回白屏

[英]calling PhP function in JavaScript returns white screen

Before anyone complains: i know that javascript is clientside and php is serverside.在有人抱怨之前:我知道 javascript 是客户端,而 php 是服务器端。 I need to call a php-function within my javascript (both in the same file).我需要在我的 javascript 中调用一个 php 函数(都在同一个文件中)。 i used我用了

document.write("<?php add(); ?>");

which calls the function from php like it should.它像它应该的那样从php调用函数。 the main problem is that the page turns white (like when i call echo "";), which should not happen.主要问题是页面变成白色(就像我调用 echo ""; 一样),这不应该发生。 i tested with an completly empty test();, ending up in the same problem.我用一个完全空的 test(); 进行了测试,最终遇到了同样的问题。

to answer the question why i want to call the function: i need to update a sql table on pressing a key.回答我为什么要调用该函数的问题:我需要在按下某个键时更新 sql 表。 as far as i know its impossible to make a keylistener in php and too unsafe to use sql in javascript.据我所知,不可能在 php 中创建一个 keylistener,并且在 javascript 中使用 sql 太不安全了。

greetings and thx for help问候和感谢帮助

When you use document.write at any other moment than during page load, it will wipe out your document and replace it with whatever you pass as argument.当您在页面加载期间以外的任何其他时刻使用document.write时,它会清除您的文档并将其替换为您作为参数传递的任何内容。 From your question it seems you run this code in an event handler, like for the keypressed event.从您的问题来看,您似乎在事件处理程序中运行此代码,例如keypressed事件。 In this case, it is expected behaviour of document.write that your page turns white.在这种情况下, document.write预期行为是您的页面变成白色。 Don't use it there.不要在那里使用它。

What you need to do is identify where in your document you want to insert content, more precisely, at which element, and provide content to it.您需要做的是确定您要在文档中的哪个位置插入内容,更准确地说,是在哪个元素上,并为其提供内容。 You can use several methods for this, like .innerHTML , but not document.write .您可以为此使用多种方法,例如.innerHTML ,但不能使用document.write

Secondly, you should understand that when you pass an argument that has php generated values in it, that these arguments are calculated at the moment php generates the page, and not when the corresponding javascript executes.其次,你应该明白,当你传递一个包含php生成值的参数时,这些参数是在php生成页面的那一刻计算的,而不是在相应的javascript执行时计算。 At that latter moment no call to php takes place, as php already did it's job during the generation of the page.在后一个时刻不会调用php ,因为php在页面生成期间已经完成了它的工作。

If you want to execute a php function at the moment of an event in the browser, you should issue an http request to the server.如果你想执行一个php浏览器事件的矩函数,你应该发出一个http请求到服务器。 This can be done by submitting a form, by navigating to a different url , or by issuing an ajax call.这可以通过提交表单、导航到不同的url或发出ajax调用来完成。 In these cases the server receives a request, and php can be executed, which will again generated content and send it back to the browser.在这些情况下,服务器收到请求,可以执行php ,它会再次生成内容并将其发送回浏览器。

You can find example ajax code on the internet, for example here .您可以在互联网上找到示例ajax代码,例如这里

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

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