如何在 PHP 中创建按钮及其事件处理程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4324315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:40:33  来源:igfitidea点击:

How to create a button and its event handler in PHP?

php

提问by Ahmad Farid

I am new to PHP but I need to create a button on a simple page and have its event handler. Any clue?

我是 PHP 新手,但我需要在一个简单的页面上创建一个按钮并拥有它的事件处理程序。有什么线索吗?

采纳答案by systemovich

In the OnClick event of the button, insert JavaScript code that points to the PHP script which you want to run when the button is clicked.

在按钮的 OnClick 事件中,插入指向单击按钮时要运行的 PHP 脚本的 JavaScript 代码。

<input type="button" value="Say Hi!" onclick="location='test.php'" />

回答by Eleazan

Ahmad Farid, how say Geoffrey Van Wyk, you can use the event "onclick" of an input element (button, for example).

Ahmad Farid,Geoffrey Van Wyk 怎么说,您可以使用输入元素(例如按钮)的“onclick”事件。

Maybe AJAX is your solution (not reload all page). A simple example of 'click' and run a script (maybe CGI, or PHP) it's here: http://www.degraeve.com/reference/simple-ajax-example.php

也许 AJAX 是您的解决方案(不是重新加载所有页面)。“单击”并运行脚本(可能是 CGI 或 PHP)的简单示例位于此处:http: //www.degraeve.com/reference/simple-ajax-example.php

Easy & useful :)

简单实用:)

PD: Sorry for my english ;(

PD:对不起我的英语;(

回答by DampeS8N

PHP doesn't work like .Net. So what you are talking about it actually not directly possible.

PHP 不像 .Net 那样工作。所以你所说的实际上并不是直接可能的。

What you will need to do is create the raw markup for your form. At minimum a form tag and your button. Set the form's action to a php script on your server, such as "button_action.php" and have it accept, via GET or POST, whatever data you wanted to send. (or if you are just talking about enacting some action, you don't need to check anything)

您需要做的是为表单创建原始标记。至少有一个表单标签和你的按钮。将表单的操作设置为您服务器上的 php 脚本,例如“button_action.php”,并让它通过 GET 或 POST 接受您想要发送的任何数据。(或者,如果您只是在谈论制定某些行动,则无需检查任何内容)

Again, this isn't .Net, so there is no postback, and you will not come back to the page. If you want to do that, you will have to redirect back from your other script.

同样,这不是 .Net,因此没有回发,您将不​​会返回该页面。如果你想这样做,你将不得不从你的其他脚本重定向回来。