php 单击html中的提交并运行php代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18177052/
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 17:06:22  来源:igfitidea点击:

click submit in html and run php code

phphtmlwordpress

提问by Sandy Stokes

I am new to html & php and I appreciate your help. I am trying to accept user input into a webpage and then after the user clicks the submit button, write a new record out to MySQL. I am using Wordpress so the tags are not exactly textbook.

我是 html 和 php 的新手,感谢您的帮助。我试图接受用户对网页的输入,然后在用户单击提交按钮后,将新记录写入 MySQL。我正在使用 Wordpress,所以标签并不完全是教科书。

So my first problem is that the submit button does not appear to execute the php code. I also would appreciate it if someone could check how I am passing my html field names to the php variables to make sure that I am on the right track. Thanks for your help!

所以我的第一个问题是提交按钮似乎没有执行php代码。如果有人可以检查我如何将我的 html 字段名称传递给 php 变量以确保我在正确的轨道上,我也将不胜感激。谢谢你的帮助!

html code to collect user input for fields on the form.....
<?php>
    if(isset($_POST['Submit'])) {
        $FName = $_POST['FName'];
        $MI = $_POST['MI'];
        $LName = $_POST['LName'];
....
?>
<form action="" method="post">
<input type="submit" name="Submit" value="Submit">

回答by Sergio

I think it can be good to read a bit more about it. Check here.

我认为阅读更多关于它的信息会很好。检查这里

Anyway, you need to say the name of the file to post to: <input type="submit" action="file.php" name="Submit" />for example.

无论如何,您需要说出要发布到的文件的名称:<input type="submit" action="file.php" name="Submit" />例如。

And you need to have more inputs than the submit.

并且您需要比提交更多的输入。

Acording to your php you should have as example this in the html:

根据您的 php,您应该在 html 中有这样的示例:

<form action="your_php_file.php" method="post">
 <p>Your first name: <input type="text" name="FName" /></p>
 <p>Your last name: <input type="text" name="LName" /></p>
 <p>Your MI: <input type="text" name="MI" /></p>
 <p><input type="submit" name="Submit"/></p>
</form>

And the php tags are like <?phpto open, and ?>to close.

而 php 标签就像<?php打开和?>关闭一样。

So like:

所以像:

<?php
    if(isset($_POST['Submit'])) {
        $FName = $_POST['FName'];
        $MI = $_POST['MI'];
        $LName = $_POST['LName'];
//....
?>

回答by federico-t

Your PHP tagsare incorrect. Use <?phpand ?>to tell PHP to start and stop interpreting the code between them. Also, make sure you're closing the formelement(I only see you're opening it) using </form>and that you indeed have all those fields contained inside of it.

您的PHP 标签不正确。使用<?php?>告诉 PHP 开始和停止解释它们之间的代码。此外,请确保您正在关闭form元素(我只看到您正在打开它)使用</form>,并且您确实在其中包含所有这些字段。