【问题标题】:Form submission in phpphp中的表单提交
【发布时间】:2018-11-04 14:46:25
【问题描述】:

我的if(isset($_POST['submit'])) 代码有点问题。我想要的是在单击表单的提交按钮时运行 processForm() 函数。问题是,当我包含if(isset($_POST['submit'])) 函数时,当我单击提交按钮时,processFunction() 根本不运行。但是当我包含if( !isset($_POST['submit'])) 时,processForm() 函数运行......为什么这是和你能帮我解决这个问题吗

<html>
    <head>
        <title>Membership Form</title>
        <style type="text/css">
            .error { background: #d33; color: white; padding: 0.2em; }
        </style>
    </head>
<body>

    <?php 
        if ( isset($_POST["submit"])) { //if submit input has value
                        processForm();//calls the processForm function
                    }

        function processForm(){ 

            $compulsaryForm = array("firstName" , "lastName", "password" , "passwordRetype");
            $missingFields = array();

            foreach ($compulsaryForm as $input) { //loop
                if (!isset($_POST[$input])) {
                    $missingFields[] = $input;
                } // forget the else part cause we mainly want to avoid the warning msg
            }

            if ($missingFields) {
                echo "<p>there are missingFields</p>";
            }

        }

     ?>

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

    *name:<input type="text" name="firstName"> <br><br>
    *last name: <input type="text" name="lastName"> <br><br>
    *password<input type="password" name="password"> <br><br>
    *retype password<input type="password" name="passwordRetype"><br><br>

    male:<input type="radio" name="sex"><br>
    female:<input type="radio" name="sex"><br>

    favourite food:<select name = "favourite">
                        <option value="select">select</option>
                        <option value="rice">rice</option>
                        <option value="beans">beans</option>
                    </select><br>

    do you want to recieve news letter?<input type="checkbox" name="newsLetter"><br>

    Any comments? :<input type="text" name="comments"><br>

    <input type="reset" name="reset">
    <input type="submit" name="submit" value="submit">

</form>

【问题讨论】:

  • 显示的代码应该可以工作(就 isset 而言)。请在此 if 之前执行 var_dump($_POST); 以查看其中的内容。
  • 旁注:你需要为你的收音机“sex”设置值。
  • 你的函数定义是在你第一次调用函数之后。尝试将函数定义移到 if-then 之前。否则 Jeff 是正确的 - 它应该工作
  • 你可以 var_dump 的 $_POST 吗,你也可以用 !empty 代替 isset
  • 旁注: 这个if (!isset($_POST[$input])) 不会像你期望的那样工作。像“”这样的空名字将被“设置”。你可能想检查empty()

标签: php arrays forms function


【解决方案1】:

您的代码在“提交”字段验证方面看起来不错。这些行看起来不太好:

$compulsaryForm = array("firstName" , "lastName", "password" , "passwordRetype");
$missingFields = array();

foreach ($compulsaryForm as $input) { //loop
  if (!isset($_POST[$input])) {
    ...

您要验证的所有输入都将在提交中发送,因此isset(...) 将为true,它们可能为空,但它们肯定存在。因此,请在服务器端或发送表单之前验证您的 emptyness,或者在这两个地方甚至更好,它应该可以工作;)

// Use: count($_POST[$input]) < 5, In case you want to validate the text length 
if ( !isset($_POST[$input]) || empty($_POST[$input]) || count($_POST[$input]) < 5 { 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多