简体   繁体   English

如何将功能连接到按钮?

[英]How to connect function to button?

I have a little contact code for user to input like so: 我有一个小的联系代码供用户输入,如下所示:

First name:<br>
    <input type = "text" name = "firstname"><br>
Last name:<br>
    <input type = "text" name = "lastname"><br>
Email:<br>
    <input type = "email" name = "email"><br>
Text:<br>
    <textarea rows = "10" cols = "50" name = "textbox"></textarea>
    <br>
    <input type = "submit" name = "submit" value = "Submit">
    <br>

and I have this php function to run if fields that user inputs are empty: 如果用户输入的字段为空,我会运行此php函数:

<?php>
if(!empty($_POST[firstname] && (!empty$_POST[lastname]) && (!empty$_POST[email]) && 
$_POST[textbox]))
{

}
?>

My question is how to connect this button "Submit" to this function and how to have a text above it saying "You need to not leave empty fields!" 我的问题是如何将此按钮“提交”连接到此功能以及如何在其上方显示“您不需要留空字段!”的文本。 for example? 例如?

Your code is missing form tags with a post method and a few brackets. 您的代码缺少带有post方法和几个括号的表单标记。

Note: I removed the ! 注意:我删除了! operator since that means "not", but you can put those back in if you want to use it another way and changing the echo message. 运算符,因为这意味着“不”,但如果您想以另一种方式使用它并更改回显消息,则可以将它们重新放入。

Also, quote the arrays since that could throw a few notices. 另外,引用数组,因为这可能会引发一些通知。

This is what your code should look like and using || 这是你的代码应该是什么样子并使用|| (OR) instead of && (AND) to check if any are empty. (OR)而不是&& (AND),以检查是否是空的。

HTML: HTML:

<form action="handler.php" method="post">
First name:<br>
    <input type = "text" name = "firstname"><br>
Last name:<br>
    <input type = "text" name = "lastname"><br>
Email:<br>
    <input type = "email" name = "email"><br>
Text:<br>
    <textarea rows = "10" cols = "50" name = "textbox"></textarea>
    <br>
    <input type = "submit" name = "submit" value = "Submit">
    <br>
</form>

PHP (handler.php): PHP(handler.php):

<?php
if(empty($_POST['firstname']) || empty($_POST['lastname']) 
   || empty($_POST['email']) || empty($_POST['textbox']))
{
    echo "Some fields were left empty.";
}
?>

Side note: You need to run this off a webserver with PHP installed with a server protocol (HTTP/HTTPS) and not directly into your browser as file:/// since that will not parse any of the PHP directives. 附注:您需要在安装了服务器协议(HTTP / HTTPS)的PHP的Web服务器上运行它,而不是直接在您的浏览器中以file:///因为它不会解析任何PHP指令。

<?php
    if($_POST['submit']){
        if( empty($_POST['firstname']) || empty($_POST['lastname']) || empty($_POST['email']) || empty($_POST['textbox']) )
        {
            echo "You need to not leave empty fields!";
        }
    }
?>

<form method="POST">
    First name:<br>
        <input type = "text" name = "firstname"><br>
    Last name:<br>
        <input type = "text" name = "lastname"><br>
    Email:<br>
        <input type = "email" name = "email"><br>
    Text:<br>
        <textarea rows = "10" cols = "50" name = "textbox"></textarea>
        <br>
        <input type = "submit" name = "submit" value = "Submit">
        <br>
</form>

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

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