检查输入值是否相等

时间:2016-02-06 16:47:31

标签: php html if-statement

我想将此代码用于条形码扫描程序,如下所示:

扫描的条形码输入insert_code输入,然后我想显示"代码没问题" ,当search_code =中的值时insert_code

我的代码在验证后会清除输入search_code,并且每次必须在search_code中重新引入相同的代码很烦人。

我可以做些什么来在每次验证后将值保持在search_code

<form action="" method="post">
    Cod I:<input type="text" name="search_code" value=""/><br/><br/>
    Cod II:<input type="" name="insert_code" value=""/><br/><br/>
    <input type="submit" name="button" value="validation" />
</form>

<?php
$search_code = $_POST ["search_code"];
$insert_code = $_POST ["insert_code"];

if ($search_code == $insert_code){
    echo "code is ok";
} else {
    echo "code is not ok";
}
?>

2 个答案:

答案 0 :(得分:1)

如果您希望保持search_code输入填充上次提交的值,只需将该帖子的值回显到输入中,如果已设置:

<form action="" method="post">
    Cod I:<input type="text" name="search_code" value="<?php echo isset($_POST['search_code'])?$_POST['search_code']:'' ?>"/><br/><br/>
    Cod II:<input type="" name="insert_code" value=""/><br/><br/>
    <input type="submit" name="button" value="validation" />
</form>

另外,为了避免有关未定义索引的警告,请将此条件添加到PHP代码中(检查是否设置了这些帖子):

<?php
if(isset($_POST ["search_code"]) && isset($_POST ["insert_code"])){
    $search_code = $_POST ["search_code"];
    $insert_code = $_POST ["insert_code"];

    if ($search_code == $insert_code){
        echo "code is ok";
    }else {
        echo "code is not ok";
    }
}
?>

答案 1 :(得分:0)

您可以在没有PHP的情况下执行此操作,这将提供更好的用户体验。这是一个实例,只是运行以查看它的工作原理:

// identify form elements:
var search_code = document.getElementById('search_code');
var insert_code = document.getElementById('insert_code');
var result = document.getElementById('result');
var button = document.getElementById('button');

// respond to button click
button.onclick = function validate() {
    // show verification result:
    result.textContent = search_code.value == insert_code.value
        ? 'code is ok'
        : 'code is not ok';
    // clear input when wrong:
    if (search_code.value !== insert_code.value) {
        insert_code.value = '';
    }
    return false;
};

insert_code.oninput = function () {
    result.textContent = ''; // clear result;
};
<form action="" method="post">
    Cod I:<input type="text" name="search_code" id="search_code" value=""/><br/><br/>
    Cod II:<input type="" name="insert_code" id="insert_code" value=""/><br/><br/>
    <input type="submit" id="button" name="button" value="validation" />
</form>
<div id="result"></div>

测试在Javascript中完成,它响应按钮点击并取消表单提交给服务器(return false)。

作为奖励,只要您在第二个输入框中键入新值,就会清除“ok / not ok”消息。

如何使用此代码

以下是代码在文档中的显示方式:

<body>
    <form action="" method="post">
        Cod I:<input type="text" name="search_code" id="search_code" value=""/><br/><br/>
        Cod II:<input type="" name="insert_code" id="insert_code" value=""/><br/><br/>
        <input type="submit" id="button" name="button" value="validation" />
    </form>
    <div id="result"></div>

    <script>
    // identify form elements:
    var search_code = document.getElementById('search_code');
    var insert_code = document.getElementById('insert_code');
    var result = document.getElementById('result');
    var button = document.getElementById('button');

    // respond to button click
    button.onclick = function validate() {
        // show verification result:
        result.textContent = search_code.value == insert_code.value
            ? 'code is ok'
            : 'code is not ok';
        // clear input when wrong:
        if (search_code.value !== insert_code.value) {
            insert_code.value = '';
        }
        return false;
    };

    insert_code.oninput = function () {
        result.textContent = ''; // clear result;
    };
    </script>
</body>

请注意,内容部分与您的内容存在一些差异:每个输入都有 id 属性,还有一个额外的 div