简体   繁体   English

如何从PHP文件在HTML内部调用警报功能

[英]How to call alert function inside HTML from a PHP file

I am trying to add reCaptcha v2 to an HTML form. 我正在尝试将reCaptcha v2添加到HTML表单中。 When the submit button is clicked, a PHP file is invoked which calls the reCaptcha API. 单击提交按钮后,将调用一个PHP文件,该文件将调用reCaptcha API。

If the reCaptcha is unsuccessful, I want to call the alert function inside the HTML page from the PHP file. 如果reCaptcha不成功,我想从PHP文件调用HTML页面内的alert函数。

I have researched a lot but I couldn't find anything for my use case. 我做了很多研究,但找不到我的用例。 This post shows how to show alert inside PHP not inside an HTML file. 这篇文章展示了如何在PHP而不是HTML文件中显示警报。

Right now when I click submit without doing reCaptcha, I've encountered a blank PHP page, but I expect to remain in the HTML form without deleting the form content and show an alert message asking the user to complete reCaptcha. 现在,当我单击提交而不执行reCaptcha时,我遇到了一个空白的PHP页面,但是我希望保留在HTML表单中而不删除表单内容,并显示一条警告消息,要求用户完成reCaptcha。

Thanks in advance! 提前致谢!

That's not how this works. 那不是这样的。 There's a separation of server and client side code. 服务器和客户端代码是分开的。 PHP runs on the server, then sends its results back to the browser on the client -- that is, it sends back the HTML / Javascript / CSS / etc. that the browser will show. PHP在服务器上运行,然后将其结果发送回客户端上的浏览器-也就是说,它将发送回该浏览器将显示的HTML / Javascript / CSS /等。 You cannot submit a form to a PHP file and then expect PHP to call a JS function on the client side. 您不能将表单提交到PHP文件,然后期望PHP在客户端调用JS函数。

You should be doing this kind of validation on the client size in the first place. 首先,您应该在客户端大小上进行这种验证。 That means you shouldn't be waiting until the data has gotten to the PHP script before validating; 这意味着您不应该等到数据到达PHP脚本后再进行验证。 you should validate that the reCaptcha is filled out with JavaScript, and if not, don't submit the form at all. 您应该验证reCaptcha是否已使用JavaScript填写,否则,请不要提交表格。

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

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