如何对表单外的输入进行php验证检查


How to do a php validation check for an input outside of a form

我有一个表单外的输入,我用javascript做验证,但我想后端验证以及。我所指的输入基本上是查看注册表单的输入方式。当输入值与javascript中的值匹配时,它会显示注册表单。

所有我想要的是找出一种方法在我的php代码中检查,看看输入的值是否与php代码中的值不匹配,这个人不能提交表单。

我所看到的一切,而试图搜索这只显示同一形式内的项目。有人知道我会怎么做吗?

假设我想让php代码检查validate input的值是否= Entry,那么我就可以提交表单了。

<div id="validate">
            <label>Please provide your code</label>
            <input type="text" id="validate-input">
        </div>
        <form action="" method="POST" id="register">
<?php if (isset($error)){
    echo $error, '<br>';
}?>
            <div class="field">
                <label for="firstname">First Name</label>
                <input type="text"  class="inputbar" name="firstname" value="<?php echo escape(Input::get('firstname')); ?>" required>
            </div>
            <div class="field">
                <label for="lastname">Last Name</label>
                <input type="text" class="inputbar" name="lastname" value="<?php echo escape(Input::get('lastname')); ?>" required>
            </div>
<input id="signinButton" type="submit" value="Register">
</form>

正如建议的那样,您可以使用AJAX调用并简单地自己序列化数据,它不需要成为表单的一部分。jQuery让它变得简单,而不是使用.serialize()方法,你可以使用'.param()'方法来代替一个对象或数组的任何你想要的参数,这里有一个例子…

脚本与形式包括…

$( document ).ready(function() {
    $('#validate-input').on('change',function(){
        var data = {}
        data.validate-input = $('#validate-input').val();
        $.ajax({ 
            cache: false, 
            url: '/validation.php', 
            type: 'POST', 
            data: jQuery.param(data),  // results in 'validate-input=abc123' string
            success: function(response){
                // 200 HTTP response - enable submit
                $('signinButton').attr('disabled',false);
                alert('Code accepted, submit away!');
            },
            error: function(response){
                // do nothing, bad code
            }
        };
    });
});
PHP脚本验证

(/validation.php)

<?php
if (isset($_POST['validate-input']) && $_POST['validate-input'] == $_SESSION['validate-code']) {
    http_response_code(200);
} else {
    http_response_code(400);
}

以上将导致event被添加到您的验证码字段,所以每次有人改变那里的值(每次他们键入一个字符),AJAX调用将在后台对服务器上的验证脚本。当代码与服务器上的代码匹配时,提交按钮将自动启用,并弹出一个窗口告诉他们可以开始了。

使用隐藏输入和onchange监听器:

<div id="validate">
    <label>Please provide your code</label>
    <!-- magic -->
    <input type="text" onchange="document.getElementById('validate-input').value = this.value;">
</div>
<form action="" method="POST" id="register">
    <!-- this gets sent to the your PHP file -->
    <input type="hidden" id="validate-input" name="validate-input">
    <?php if (isset($error)){
        echo $error, '<br>';
    }?>
    <div class="field">
        <label for="firstname">First Name</label>
        <input type="text"  class="inputbar" name="firstname" value="<?php echo escape(Input::get('firstname')); ?>" required>
    </div>
    <div class="field">
        <label for="lastname">Last Name</label>
        <input type="text" class="inputbar" name="lastname" value="<?php echo escape(Input::get('lastname')); ?>" required>
    </div>
    <input id="signinButton" type="submit" value="Register">
</form>