php 一个按钮来启动php脚本,如何?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1697484/
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
A button to start php script, how?
提问by Strawberry
I want to make a button that starts my php script after I click it. So do I just make 2 separate files and have action post to the php file and then let it start? or is there a better way to do this? Possibly in one document?
我想制作一个按钮,在我点击它后启动我的 php 脚本。那么我是否只制作 2 个单独的文件并将操作发布到 php 文件,然后让它启动?或者有更好的方法来做到这一点?可能在一份文件中?
Update:
更新:
Well, I basically made a script that would do a series of a loops until it's finished. So usually when I visit the page it automatically starts, so I'm making a button to start it only when I need it.
好吧,我基本上制作了一个脚本,它会执行一系列循环直到完成。所以通常当我访问该页面时它会自动启动,所以我制作了一个按钮以仅在需要时启动它。
More info: Answer to one of the questions, "starting the script" as in it would only execute the script.
更多信息:回答其中一个问题,“启动脚本”,因为它只会执行脚本。
More info:I really don't need to pass any data from the submit form to the php script. I just want my script to run when I hit a button. I just want to know what's the best way to do this.
更多信息:我真的不需要将任何数据从提交表单传递到 php 脚本。我只希望我的脚本在我点击按钮时运行。我只想知道这样做的最佳方法是什么。
回答by Carlos Lima
Having 2 files like you suggested would be the easiest solution.
像您建议的那样拥有 2 个文件将是最简单的解决方案。
For instance:
例如:
2 files solution:
2个文件解决方案:
index.html
索引.html
(.. your html ..)
<form action="script.php" method="get">
<input type="submit" value="Run me now!">
</form>
(...)
script.php
脚本文件
<?php
echo "Hello world!"; // Your code here
?>
Single file solution:
单文件解决方案:
index.php
索引.php
<?php
if (!empty($_GET['act'])) {
echo "Hello world!"; //Your code here
} else {
?>
(.. your html ..)
<form action="index.php" method="get">
<input type="hidden" name="act" value="run">
<input type="submit" value="Run me now!">
</form>
<?php
}
?>
回答by DuckPuncher
I know this question is 5 years old, but for anybody wondering how to do this without re-rendering the main page. This solution uses the dart editor/scripting language.
我知道这个问题已经有 5 年的历史了,但是对于想知道如何在不重新呈现主页的情况下做到这一点的人来说。此解决方案使用dart 编辑器/脚本语言。
You could have an <object>tag that contains a dataattribute. Make the <object>1px by 1px and then use something like dartto dynamically change the <object>'s dataattribute which re-renders the datain the 1px by 1px object.
您可以有一个<object>包含data属性的标签。使<object>1px x 1px,然后使用类似dart 的东西动态更改<object>'sdata属性,该属性data在 1px x 1px 对象中重新呈现。
HTML Script:
HTML 脚本:
<object id="external_source" type="text/html" data="" width="1px" height="1px">
</object>
<button id="button1" type="button">Start Script</button>
<script async type="application/dart" src="dartScript.dart"></script>
<script async src="packages/browser/dart.js"></script>
someScript.php:
someScript.php:
<?php
echo 'hello world';
?>
dartScript.dart:
dartScript.dart:
import 'dart:html';
InputElement button1;
ObjectElement externalSource;
void main() {
button1 = querySelector('#button1')
..onClick.listen(runExternalSource);
externalSource = querySelector('#external_source');
}
void runExternalSource(Event e) {
externalSource.setAttribute('data', 'someScript.php');
}
So long as you aren't posting any information and you are just looking to run a script, this should work just fine.
只要您不发布任何信息并且只想运行脚本,这应该可以正常工作。
Just build the dart script using "pub Build(generate JS)" and then upload the package onto your server.
只需使用“pub Build(generate JS)”构建 dart 脚本,然后将包上传到您的服务器。
回答by Myles
You could do it in one document if you had a conditional based on params sent over. Eg:
如果您有基于发送的参数的条件,您可以在一个文档中完成。例如:
if (isset($_GET['secret_param'])) {
<run script>
} else {
<display button>
}
I think the best way though is to have two files.
我认为最好的方法是有两个文件。
回答by jkndrkn
What exactly do you mean by "starts my php script"? What kind of PHP script? One to generate an HTML response for an end-user, or one that simply performs some kind of data processing task? If you are familiar with using the tag and how it interacts with PHP, then you should only need to POST to your target PHP script using an button of type "submit". If you are not familiar with forms, take a look here.
“启动我的php脚本”到底是什么意思?什么样的PHP脚本?是为最终用户生成 HTML 响应,还是仅执行某种数据处理任务?如果您熟悉标签的使用以及它如何与 PHP 交互,那么您应该只需要使用“提交”类型的按钮 POST 到您的目标 PHP 脚本。如果您不熟悉表格,请看这里。
回答by Alireza
This one works for me:
这个对我有用:
index.php
索引.php
<?php
if(isset($_GET['action']))
{
//your code
echo 'Welcome';
}
?>
<form id="frm" method="post" action="?action" >
<input type="submit" value="Submit" id="submit" />
</form>
This link can be helpful:
此链接可能会有所帮助:

