简体   繁体   English

从 HTML 输入按钮调用 php 函数或文件

[英]call a php function or file from a HTML input button

What is the best way to implement php code to run when a button is pressed/clicked.在按下/单击按钮时实现 php 代码运行的最佳方法是什么。 Done some research but couldn't find anything relevant apart of few examples that show use of JavaScript.做了一些研究,但除了几个展示 JavaScript 使用的例子外,找不到任何相关的东西。 I want it to be purely in PHP.我希望它纯粹是在 PHP 中。 At this precise moment I have created the buttons in the following way <input type='button' value='Click Me'/> now not sure what to do.在这个精确的时刻,我以以下方式创建了按钮<input type='button' value='Click Me'/>现在不知道该怎么做。 Could anyone help please.任何人都可以请帮忙。

<form action="index.php" method="post">
    <input type='button' name='btn_func' value='Click Me'/>
</form>

<?php
    if(isset($_POST['btn_func'])){
        //call the function here.
    }
?>

If you have 10 buttons:如果您有 10 个按钮:

<form action="index.php" method="post">
    <input type='button' name='btn_func_1' value='Click Me 1'/>
    <input type='button' name='btn_func_2' value='Click Me 2'/>
    <input type='button' name='btn_func_3' value='Click Me 3'/>
    <input type='button' name='btn_func_4' value='Click Me 4'/>
    <input type='button' name='btn_func_5' value='Click Me 5'/>
    <input type='button' name='btn_func_6' value='Click Me 6'/>
    <input type='button' name='btn_func_7' value='Click Me 7'/>
    <input type='button' name='btn_func_8' value='Click Me 8'/>
    <input type='button' name='btn_func_9' value='Click Me 9'/>
    <input type='button' name='btn_func_10' value='Click Me 10'/>
</form>

<?php
    if(isset($_POST['btn_func_1'])){
        //call the function here.
    }
?>

Or use a Conditional/Switch statement.或者使用条件/开关语句。

I don't think you can run a PHP script by native HTML only.我认为您不能仅通过本机 HTML 运行 PHP 脚本。 Using JavaScript would be the simplest solution you can do.使用 JavaScript 将是您能做的最简单的解决方案。

The basics of a form and php.表单和php的基础知识。 when the button is pushed, the form calls an action (some page, even itself).当按钮被按下时,表单调用一个动作(某些页面,甚至它本身)。 Then the php checks if the button was pushed.然后 php 检查按钮是否被按下。 If so, do something, if not, ignore it.如果是这样,做一些事情,如果不是,忽略它。 Instead of creating a button though, a submit would work better, but the button works just fine.虽然不是创建一个按钮,提交会更好地工作,但按钮工作得很好。

<form method="post" action="./PHPScripts.php" />
     <input type='button' value='Click Me' name='button' /> <-- added a name
     <input type='submit' value='Click Me' name='submitButton' />
     <input type='submit' value='Click Me' name='submitButton2' />
</form>


// below would be on the page called by the action in the above form
// which can be on the same page with no problem, but larger amounts of
// code become more difficult to read, so having multiple files is recommended
<?php
     if($_POST['button']) {
        // php code (either execute it, or call function)
     }
     if($_POST['submitButton']) {
        // php code (either execute it, or call function)
     }
     if($_POST['submitButton2']) {
        // php code (either execute it, or call function)
     }
?>

It's best to keep php code on top of html if on same page, but for demonstration reasons, I've put it on the bottom.如果在同一页面上,最好将 php 代码放在 html 的顶部,但出于演示原因,我把它放在了底部。 (Easier to read) (更容易阅读)

If you are going to use input type="button" , I am assuming you will need JavaScript to handle the click event?如果您打算使用input type="button" ,我假设您需要 JavaScript 来处理点击事件? Then make an Ajax request to a PHP file or something?然后向 PHP 文件或其他内容发出 Ajax 请求?

Or, you could change your input type from "button" to "submit" <input type="submit" name="submitButton" id="submitButton" value="Submit" />或者,您可以将输入类型从“按钮”更改为“提交” <input type="submit" name="submitButton" id="submitButton" value="Submit" />

Then the...easiest (for lack of better words) is to check at the top of your page for the submit post variable.然后……最简单的(因为没有更好的词)是在页面顶部检查提交帖子变量。 If it's there, PHP will handle it.如果它在那里,PHP 会处理它。 Or, it will display the form if the variable is not set.或者,如果未设置变量,它将显示表单。

<?php
    if (isset($_POST['submitButton']))
    {
        // form has been submitted.
        // do something with PHP.

    }
?>
<form action="file.php" method="post">
    ...
    <input type="submit" name="submitButton" id="submitButton" value="Submit" />
</form>

The idea is, you will need to give the button the name attribute.这个想法是,您需要为按钮指定名称属性。 Depending on how the form, is then submitted, in this case, to itself or to another page determined by the form action attribute and the method, the script will then run.根据表单提交的方式,在这种情况下,提交给表单本身或由表单操作属性和方法确定的另一个页面,然后脚本将运行。

<form action="submit.php" method="post"> 
<input type='button' value='Click Me' **name="submit"**/> 
</form>

if(isset($_POST['submit']) {
     //runs when the button is clicked
}

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

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