简体   繁体   English

当我提交 HTML 表单时,我的 PHP 代码如何运行

[英]How can my PHP code run when i submit a HTML form

I am trying to make a website using python and in flask I have a webpage that has a submit button, when the button is pressed I need it to run a python file I have called Answers.py我正在尝试使用 python 创建一个网站,在 Flask 中,我有一个带有提交按钮的网页,当按下按钮时,我需要它来运行一个名为 Answers.py 的 python 文件

from flask import Flask, request, render_template, send_from_directory

app = Flask(__name__)
@app.route('/help', methods=["GET","POST"])
def help():
    return render_template("help.html")

the HTML code on the page is this and i am trying to get when the button is pressed it runs a php script that outputs the value from a separate python file, the output text.页面上的 HTML 代码是这样的,我试图在按下按钮时获取它运行一个 php 脚本,该脚本从单独的 python 文件输出值,即输出文本。

<html>
<body>
<form action="" method="post">
    <button type="submit" name="sub" value="Hello world">Submit 
call</button>
</form>
</body>
</html>

I have tried this php code in the same file as the HTML but nothing is output when I press the button, i'm new to php so i'm not sure what the problem is.我已经在与 HTML 相同的文件中尝试过这个 php 代码,但是当我按下按钮时没有任何输出,我是 php 新手,所以我不确定问题是什么。

<?php
if(isset($_POST['sub']))
    {
        $result = exec("Python Path Answers.py /tmp");
        echo $result;
    }
?>

The php script never gets referenced in the html page! php 脚本永远不会在 html 页面中被引用!

<form action="" method="post"><!-- action there is empty! -->

You should reference the php script in the action attribute of the <form> tag, like so:您应该在<form>标签的action属性中引用 php 脚本,如下所示:

<form action="myscript.php" method="post">

暂无
暂无

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

相关问题 我的HTML表单将其操作链接到php代码。 但是,当单击“提交”按钮时,表单将重定向到PHP文件。 我该如何预防? - My HTML form has its action linked to php code. When the submit button is clicked though, the form redirects to the PHP file. How can I prevent this? 当我在提交表单中调用它时,我的 php 函数无法运行 - my php function cant run when i call it in submit form 用户使用html提交数据时如何运行PHP代码 - How do I run a PHP code when user submit data with html 单击1个表单中的2个提交按钮中的1个,如何运行脚本? - How can I run a script when 1 of my 2 submit buttons within 1 form is clicked? 如何通过1个提交按钮将html / php代码定向到两个不同的页面? - How can I get my html/php code to direct to two different pages from 1 submit button? 如何将一个HTML表单提交到多个php文件? - How can I submit one HTML form to multiple php files? 我如何在php中提交我的表单以回应响应? - How can i submit my form in php to echo a response? 如何将我的 jquery 步骤表单提交到 PHP 文件? - How I can submit my jquery steps form to a PHP file? 我创建了一个html提交按钮,并与php代码连接,但是,选择之后,我该如何返回我的html页面? - I have created one html submit button and connects with php code but, after selecting that how can i go back my html page? 如何将HTML表单提交与JavaScript表单更新分开? - How can I separate my HTML form submit from my JavaScript form update?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM